简体   繁体   English

使用 Django API 处理并发请求

[英]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/ ).我想用 DRF ( https://www.django-rest-framework.org/ ) 创建一个简单的 Rest API。 Another service is going to make requests in which it provides a JSON.另一个服务将发出请求,并在其中提供 JSON。 This json is parsed and some rather simple pandas dataframe operations are going to take place.这个 json 被解析,一些相当简单的 Pandas 数据框操作将发生。 The result is sent back via JSON.结果通过 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.尽管 Pandas 操作相当简单,但它们仍然可能需要大约 0.5s-1s,如果由于某种原因在同一时刻发出 3-4 个这样的请求,我想避免人们等待几秒钟。

Thanks!谢谢!

Did you deploy your API using YourAPIScript.py runserver ?您是否使用YourAPIScript.py runserver部署了您的 API?

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.如果您使用更面向生产的 WSGI 服务器,例如 uwsgi,则可以将其设置为一次处理多个请求。 Try this tutorial from the Django docs: How to use Djanog with uwsgi试试 Django 文档中的这个教程: 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 ).如果您使用 gunicorn,您可以增加工作线程和线程的数量,或者将工作线程类切换为非阻塞的单geventeventlet ( 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.对于工作线程数和线程数,默认值为 1,这意味着默认情况下您会获得 1 个请求的并发性。
  3. You can also use a task queue for this.您也可以为此使用任务队列。 Most people use celery .大多数人使用芹菜

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;客户端向 API 发送 JSON;
  2. API creates Celery task to process JSON, then returns some sort of id or URL where the result will be stored; API 创建Celery 任务来处理 JSON,然后返回某种 id 或 URL 来存储结果;
  3. Celery task processes JSON in background; Celery 任务在后台处理 JSON;
  4. When result is ready, the external service can grab it using id or URL from step 2.当结果准备好时,外部服务可以使用第 2 步中的 id 或 URL 获取它。

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.在我看来,在请求-响应过程中使用 Pandas 处理 JSON 不是您想要做的事情,它更适合异步任务架构。 Today it may take 0,5-1 seconds, but it's possible that tomorrow it will take 10 seconds and hang your application今天可能需要 0.5-1 秒,但明天可能需要 10 秒并挂起您的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM