0
I am researching about parallel execution in Python and I was left with a question that I have not found clear answer.
Let’s say I want to multiply two matrices and add in a task list where each task is a row of matrix A and a column of matrix B and ask that n
threads remove from this list a task to multiply.
I have 4 colors on the machine, using the module threading.Thread
from Python, if I create 4 theads for the execution of the task list, these threads one shall be carried out in each core? I mean, parallel? Or for that I would need to use the module multiprocessing
that divides the tasks into 1 process for each core?
If only with multiprocessing
I can distribute the tasks in the 4 colors, how do I set, for example, 2 threads in each process, that is to say each core would receive 2 threads for processing my task?
Something I already recommend you research is about GIL (Global Interpreter Lock), in Python, which briefly says that the interpreter can only be used by one thread at a time.
– Woss
Have you tried anything? Add your question what you have tried
– R.Santos
I haven’t developed yet I’m trying to understand how to use the 4 processors of the machine with python, I don’t know if by creating 4 threads automatically each one will run on each processor.
– André Salgueiro
@Andrésalgueiro The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero
In fact, in pure Python, only one thread runs at a time, no matter how many nuclei you have. Numpy is no longer pure Python - and in fact, it already parallelizes this type of operation on several cores alone. For didactic purposes yes, there is a lot to talk about. I have some answers already about multiprocessing
– jsbueno
These two answers have a lot of detail of what you need to understand now - both how Python works with multi-threading and multi-processing, how to do it. https://answall.com/questions/146417/fibonacci-com-execu%C3%A7%C3%a3o-parallel-threads/146458#146458 https://answall.com/questions/197174/python-c%C3%b3digo-serial-vs-multiprocessing-vs-threading/197678#197678
– jsbueno