Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of multithreading in Python if the GIL exists?

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.

like image 283
coolster1278 Avatar asked Sep 25 '18 22:09

coolster1278


People also ask

How does Python multithreading work with GIL?

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.

Is there a point to multithreading 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.

What is the point of threading Python?

Threading in python is used to run multiple threads (tasks, function calls) at the same time. Note that this does not mean that they are executed on different CPUs. Python threads will NOT make your program faster if it already uses 100 % CPU time.

Will Python ever remove the GIL?

Don't expect Python 3.11 to drop the GIL just yet. Merging Sam's work back to CPython will itself be a laborious process, but is only part of what's needed: a very good backwards compatibility and migration plan for the community is needed before CPython drops the GIL. None of this is planned yet.


2 Answers

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.

like image 123
Roman Konoval Avatar answered Sep 22 '22 14:09

Roman Konoval


Strictly speeaking, CPython support multi-io-bound-thread + single-cpu-bound-thread

io bound method: file.open, file.write, file.read, socket.send, socket.recv, etc. when python call these io function, it will release GIL and acquire GIL after io function return implicitly

cpu bound method: arithmatic calculation, etc.

c extension method: method must call PyEval_SaveThread & PyEval_RestoreThread explicitly to tell the python interpreter what you are doing

like image 31
Yessy Avatar answered Sep 21 '22 14:09

Yessy