Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gunicorn gevent workers vs Uvicorn ASGI

I'm currently developing a service in Django which makes use of a slow external API (takes about 10s to get a response), which means the connections to my server are kept open waiting for the external API to respond, and occupying worker time/resources.

I know I can use gunicorn's thread or gevent workers to add concurrency, but can't seem to grasp the exact difference between using gunicorn with gevent workers and uvicorn (or any other server) with the asgi interface.

What would be the criteria for using one over the other?

Django still doesn't fully support async/await views. Would it be better if I just stick with gevent workers?

like image 380
imricardoramos Avatar asked Jun 23 '20 20:06

imricardoramos


People also ask

Should I use Gunicorn or Uvicorn?

Nevertheless, as of now, Uvicorn's capabilities for handling worker processes are more limited than Gunicorn's. So, if you want to have a process manager at this level (at the Python level), then it might be better to try with Gunicorn as the process manager.

What is difference between Uvicorn and Gunicorn?

Gunicorn is a mature, fully featured server and process manager. Uvicorn includes a Gunicorn worker class allowing you to run ASGI applications, with all of Uvicorn's performance benefits, while also giving you Gunicorn's fully-featured process management.

Does Gunicorn work with ASGI?

Although Gunicorn is traditionally used to deploy WSGI applications, it also provides a pluggable interface to provide ASGI deployment. It does this by allowing you to consume a worker class exposed by the ASGI server ( uvicorn ).

Does Gunicorn use gevent?

By default, Gunicorn uses a synchronous worker class to serve requests, but it can be easily configured to use gevent by simply adding -k gevent to the run command.


1 Answers

Gunicorn has a pre-fork worker model

A pre-fork worker model basically means a master creates forks which handle each request. A fork is a completely separate *nix process (Source).

Uvicorn is a ASGI server running uvloop

Python async needs a event loop for it to use it's async features. And uvloop is an alternative to the asyncio loop. So if you're having async calls in your code you could use uvloop internally or use uvicorn as your ASGI server. Note: You still have just a single event loop. You can choose to use gunicorn and uvicorn to have multiple workers and use uvloop.

In your django application it makes sense to use uvloop internally (if you choose to) and use gunicorn as your ASGI.

like image 98
clamentjohn Avatar answered Oct 01 '22 00:10

clamentjohn