[英]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:解决方案:
gevent
or eventlet
( http://docs.gunicorn.org/en/stable/settings.html#id75 ).gevent
或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.In my opinion it may be a good idea to avoid solving this problem with multi-threading.在我看来,避免用多线程解决这个问题可能是个好主意。 It may work better this way:
这样做可能会更好:
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.