Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does python handle thread locking / context switching?

I watched an excellent presentation on the GIL, and how when running in the interpreter only 1 single thread can run at a time. It also seemed that python is not very intelligent about switching between threads.

If i am threading some operation that only runs in the interpreter, and it is not particularly CPU heavy, and I use a thread lock where only 1 thread can run at a time for this relatively short interpreter-bound operation, will that lock actually make anything run slower? as opposed to if the lock were not necessary and all threads could run concurrently.

If all but 1 threads are locked, will the python interpreter know not to context switch?

Edit: by 'making things run slower' I mean if python is context switching to a bunch of locked threads, that will (maybe) be a performance decrease even if the threads don't actually run

like image 655
e wagness Avatar asked Oct 26 '15 17:10

e wagness


People also ask

How does Python thread lock work?

A lock can be locked using the acquire() method. Once a thread has acquired the lock, all subsequent attempts to acquire the lock are blocked until it is released. The lock can be released using the release() method. Calling the release() method on a lock, in an unlocked state, results in an error.

What is context switching in multithreading in Python?

A thread is a unit of execution within a process. Multithreading refers to concurrently executing multiple threads by rapidly switching the control of the CPU between threads (called context switching).

Does Python use real threads if it uses a global interpreter lock?

The Python Global Interpreter Lock or GIL, in simple words, is a mutex (or a lock) that allows only one thread to hold the control of the Python interpreter. This means that only one thread can be in a state of execution at any point in time.

What is context switching Python?

An operating system uses this technique to switch a process between states to execute its functions through CPUs. It is a process of saving the context(state) of the old process(suspend) and loading it into the new process(resume). It occurs whenever the CPU switches between one process and another.


2 Answers

Larry Hastings (a core CPython Developer) has a great talk that covers this subject called "Python's Infamous GIL". If you skip to 11:40ish he gives the answer to your question.

From the talk: The way Python threads work with the GIL is with a simple counter. With every 100 byte codes executed the GIL is supposed to be released by the thread currently executing in order to give other threads a chance to execute code. This behavior is essentially broken in Python 2.7 because of the thread release/acquire mechanism. It has been fixed in Python 3.

When you use a thread lock Python will only execute the threads that are not locked. So if you have several threads sharing 1 lock, then only one thread will execute at the same time. Python will not start executing a locked thread until the thread can acquire the lock. Locks are there so you can have shared state between threads without introducing bugs.

If you have several threads and only 1 can run at a time because of a lock, then in theory your program will take longer to execute. In practice you should benchmark, because the results will surprise you.

like image 81
tipsqueal Avatar answered Oct 05 '22 18:10

tipsqueal


python is not very intelligent about switching between threads

Python threads work a certain way :-)

if I use a thread lock where only 1 thread can run at a time... will that lock actually make anything run slower

Err, no because there is nothing else runnable, so nothing else could run slower.

If all but 1 threads are locked, will the python interpreter know not to context switch?

Yes. The kernel knows which threads are runnable. If no other threads can run then logically speaking (as far as the thread is concerned) the python interpreter won't context switch away from the only runnable thread. The thread doesn't know when it has been switched away from (how can it, it isn't running).

like image 32
DisappointedByUnaccountableMod Avatar answered Oct 05 '22 18:10

DisappointedByUnaccountableMod