Bishonen_PL
Bishonen_PL

Reputation: 1571

Handling concurrent requests with Django API

I'd like to make a create a simple Rest API with DRF (https://www.django-rest-framework.org/). Another service is going to make requests in which it provides a JSON. This json is parsed and some rather simple pandas dataframe operations are going to take place. The result is sent back via JSON.

Is there a way to make this process multi-threaded? Even though the pandas operations are rather simple, they still might take ~0.5s-1s, and I'd like to avoid people waiting for a few secs if there's 3-4 of such requests made in the same moment for some reason.

Thanks!

Upvotes: 3

Views: 5940

Answers (2)

Karthick Mohanraj
Karthick Mohanraj

Reputation: 1658

Did you deploy your API using YourAPIScript.py runserver?

As far as I remember, it's single threaded, so it won't be able to start processing one request until it has finished processing another.

Solutions:

  1. If you use a more production-oriented WSGI server, like uwsgi, it can be set up to serve more than one request at a time. Try this tutorial from the Django docs: How to use Djanog with uwsgi
  2. If you use gunicorn you can either increase the number of workers and threads or switch the worker class to a non-blocking one gevent or eventlet (http://docs.gunicorn.org/en/stable/settings.html#id75). For worker count and threads, the defaults are 1 each, meaning that by default you get a concurrency of 1 request.
  3. You can also use a task queue for this. Most people use celery.

Upvotes: 7

Vsevolod Skripnik
Vsevolod Skripnik

Reputation: 86

In my opinion it may be a good idea to avoid solving this problem with multi-threading. It may work better this way:

  1. Client sends JSON to API;
  2. API creates Celery task to process JSON, then returns some sort of id or URL where the result will be stored;
  3. Celery task processes JSON in background;
  4. When result is ready, the external service can grab it using id or URL from step 2.

In my opinion processing JSON with Pandas is not something you would want to do during request-response process, it better fits asynchronous tasks architecture. Today it may take 0,5-1 seconds, but it's possible that tomorrow it will take 10 seconds and hang your application

Upvotes: 3

Related Questions