Python and multi-threading. Is it a good idea? List some ways to get some Python code to run in a parallel way. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Python and multi-threading. Is it a good idea? List some ways to get some Python code to run in a parallel way.

Can you please answer this question? I have to give the answer to my friend..

10th May 2017, 5:42 AM
Ajay singh
Ajay singh - avatar
2 Answers
+ 6
I'd recommend to use the multiprocessing package.
10th May 2017, 5:52 AM
Tob
Tob - avatar
+ 1
Python doesn't allow multi-threading. Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your 'threads' can execute at any one time. A thread acquires the GIL, does a little work, then passes the GIL onto the next thread. This happens very quickly so to the human eye it may seem like your threads are executing in parallel, but they are really just taking turns using the same CPU core. All this GIL passing adds overhead to execution. This means that if you want to make your code run faster then using the threading package often isn't a good idea. If you want to run some things simultaneously, and efficiency is not a concern, then it's totally fine and convenient.
23rd Jun 2017, 6:36 AM
Vishnu Sivan
Vishnu Sivan - avatar