Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a python web server overcomes GIL

Any web server might have to handle a lot of requests at the same time. As python interpreter actually has GIL constraint, how concurrency is implemented?

Do they use multiple processes and use IPC for state sharing?

like image 715
Senthil Babu Avatar asked Sep 26 '12 14:09

Senthil Babu


People also ask

Does Python still have GIL?

The GIL's low performance overhead really shines for single-threaded operations, including I/O-multiplexed programs where libraries like asyncio are used, and this is still a predominant use of Python.

Can Python be used for web server?

You must know that Python can be used to write web servers very effectively. It is known that there are many popular and excellent frameworks and libraries such as Django and Flask, which allows backend developers to focus on the business logic and save a lot of time on coding.

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

The conclusion here is that it's possible to speed up CPU-intensive Python code using multithreading if the code calls C functions that release the GIL. Note that such functions can be found not only in the standard library but also in computational-heavy third-party modules like NumPy.


1 Answers

You usually have many workers(i.e. gunicorn), each being dispatched with independent requests. Everything else(concurrency related) is handled by the database so it is abstracted from you.

You don't need IPC, you just need a "single source of truth", which will be the RDBMS, a cache server(redis, memcached), etc.

like image 120
rantanplan Avatar answered Oct 06 '22 09:10

rantanplan