36

From what I understand, the GIL makes it impossible to have threads that harness a core each individually.

This is a basic question, but, what is then the point of the threading library? It seems useless if the threaded code has equivalent speed to a normal program.

coolster1278
  • 471
  • 4
  • 7
  • 2
    It can be used to unblock the main thread (for example GUI application or similar). If you want to use multiple cores, you should try multiprocessing (https://docs.python.org/3.7/library/multiprocessing.html) –  Sep 25 '18 at 23:15
  • 1
    Try [this question](https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python). Short answer: it can useful, but maybe not in the way you're imagining. Only one thread can process Python at a time due to the GIL, meaning threaded programs still run serially. The `multiprocessing` library is more helpful to what you seem to be looking for, as it can actually spawn processes that harness individual cores. – questionable_code Sep 25 '18 at 23:15
  • Thanks @questionable_code and @Tom for your help. I'm looking into multi-processing and I'll probably have to use it. I'm still curious to why they even have the `threading` library. It seems it's more for code organization. – coolster1278 Sep 25 '18 at 23:22
  • See this (http://dabeaz.com/python/UnderstandingGIL.pdf) and this (http://www.dabeaz.com/python/GIL.pdf) talks, quite interesting. It seems that multithreaded programs work much faster on 1 core than on 2 or 4. The talks are quite old (2010) and there's some mention of a new GIL in Python 3.x, but I didn't try it. –  Sep 25 '18 at 23:32

2 Answers2

22

In some cases an application may not utilize even one core fully and using threads (or processes) may help to do that.

Think of a typical web application. It receives requests from clients, does some queries to the database and returns data back to the client. Given that IO operation is order of magnitude slower than CPU operation most of the time such application is waiting for IO to complete. First, it waits to read the request from the socket. Then it waits till the request to the database is written into the socket opened to the DB. Then it waits for response from the database and then for response to be written to the client socket.

Waiting for IO to complete may take 90% (or more) of the time the request is processed. When single threaded application is waiting on IO it just not using the core and the core is available for execution. So such application has a room for other threads to execute even on a single core.

In this case when one thread waits for IO to complete it releases GIL and another thread can continue execution.

  • So, one can conclude that the python threading module is useful when writing IO bound programs. Is that correct? Are there other situations to consider? – ahfx May 01 '22 at 18:35
3

Strictly speaking, CPython supports multi-io-bound-thread + single-cpu-bound-thread.

  • I/O bound method: file.open, file.write, file.read, socket.send, socket.recv, etc. When Python calls these I/O functions, it will release GIL and acquire GIL after I/O function returns implicitly.

  • CPU bound method: arithmetic calculation, etc.

  • C extension method: method must call PyEval_SaveThread and PyEval_RestoreThread explicitly to tell the Python interpreter what you are doing.

Nat Riddle
  • 911
  • 1
  • 9
  • 21
Yessy
  • 918
  • 1
  • 7
  • 12