+ 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..
2 Answers
+ 6
I'd recommend to use the multiprocessing package.
+ 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.