39

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?

imricardoramos
  • 846
  • 1
  • 7
  • 12

1 Answers1

16

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.

clamentjohn
  • 3,417
  • 2
  • 18
  • 42
  • to add parallelism in a mostly cpu-bound bound application and use resources to the maximum for maximum throughput, which gunicorn worker do I use? my use case is to serve sklearn model with fastapi in k8s. – Naveen Reddy Marthala Jan 15 '22 at 10:05