1

In the Python documentation about threads and the GIL, it says:

In order to emulate concurrency of execution, the interpreter regularly tries to switch threads (see sys.setswitchinterval())

Why would it do this? These context switches appear to do nothing other than waste time. Wouldn't it be quicker to run each process until it releases the GIL, and then run the next?

7
  • It makes a difference if some of your "threads" (green threads) are stuck doing IO operations. Commented Jan 18, 2019 at 15:33
  • You are misunderstanding the docs. sys.setswitchinterval is setting the interval a thread maximally would hold the GIL. @devoured elysium Python-threads are real OS-threads, not green-threads. Commented Jan 18, 2019 at 15:41
  • @Darkonaut but why have a maximum time that a thread can hold the GIL if it is automatically released in the event of blocking I/O? Why would you want more context switching? Why bother 'emulating concurrency' if it slower? Commented Jan 18, 2019 at 15:54
  • Because then you would have only context switches on I/O. A thread doesn't necessarily have any I/O. If you want to trade simplicity for more efficiency you'll have to look into asyncio for single-threaded concurrency. Commented Jan 18, 2019 at 16:01
  • 1
    You could have one thread doing number crunching, another handling I/O. The number-chrunching thread with your proposal would never drop the GIL so the other thread could handle the I/O. Commented Jan 18, 2019 at 16:06

1 Answer 1

4

A thread doesn't neccessarly have any I/O. You could have one thread doing number crunching, another handling I/O. The number-crunching thread with your proposal would never drop the GIL so the other thread could handle the I/O.

To ensure every thread gets to run, a thread will by default drop the GIL after 5 ms (Python 3) if it hasn't done so before because of waiting for I/O.

You can change this interval with sys.setswitchinterval().

Threading is a simple concurrency technique. For a more efficient concurrency technique look into asyncio which offers single-threaded concurrency using coroutines.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.