Search code examples
pythonmultithreadingpython-multithreading

When does Python switch between Threads when using the threading module?


Here's how I understand it (please correct me if I'm wrong): Python switches between Threads in certain time intervals. Additionally when waiting for an I/O Operation Python releases the GIL so that another thread can be processed.

Here's what I don't understand:

  • How is the time interval for switching between Threads determined?
  • How does Python know that a thread is waiting for an I/O Operation? (Some libraries like requests do not work with asyncio but when using threading, Python somehow knows when they're waiting for I/O)
  • If Python can somehow by itself determine if it is waiting for I/O, why would someone ever use asyncio?

Solution

  • Python doesn't switch threads. Python uses native threads, which means, it's the operating system that decides when to "switch." (I put "switch" in scare quotes because it's entirely possible for different Python threads to be running on different CPUs.)

    How is the time interval for switching between Threads determined?

    That depends on what OS the program is running on, and maybe on what "scheduling policy" is in effect for the process.

    How does Python know that a thread is waiting for an I/O Operation?

    The operating system knows that a thread is waiting for I/O because the thread made an I/O system call, and it's the operating system's job to complete the operation.

    Python potentially could know because the thread called some Python I/O function that actually made the system call.

    If Python can somehow by itself determine if it is waiting for I/O, why would someone ever use asyncio?

    I haven't used "async" myself, I was always an old-school threading guy, but it's probably a matter of taste. "Async" and "threading" are two different model of concurrency. Whichever one you're more familiar with probably is the one you're gonna use until one of your co-workers persuades you to try the other one.