简体   繁体   English

Celery + Eventlet +非阻塞请求

[英]Celery + Eventlet + non blocking requests

I am using Python requests in celery workers to make large number of (~10/sec) API calls(includes GET,POST, PUT, DELETE). 我在芹菜workers使用Python requests来进行大量(~10 /秒)API调用(包括GET,POST,PUT,DELETE)。 Each request takes around 5-10s to complete. 每个请求大约需要5到10秒才能完成。

I tried running celery workers in eventlet pool, with 1000 concurrency. 我尝试在eventlet池中运行芹菜工作eventlet ,具有1000个并发性。

Since requests are blocking process each concurrent connection is waiting on one request. 由于requests是阻塞进程,因此每个并发连接都在等待一个请求。

How do I make requests asynchronous? 如何异步requests

Use eventlet monkey patching to make any pure python library non-blocking. 使用eventlet monkey补丁使任何纯python库无阻塞。

  • patch single library 补丁单库

     # import requests # instead do this: import eventlet requests = eventlet.import_patched('requests') 

    packages erequests and grequests could be stripped down to these two lines. erequestsgrequests可以被剥离到这两行。

  • patch everything 一切都好

     import eventlet eventlet.monkey_patch() # must execute as early as possible ... # everything is non-blocking now: import requests, amqp, memcache, paramiko, redis 

Update : there is known issue with monkey patching requests library. 更新 :猴子修补请求库存在已知问题 If you get: 如果你得到:

ImportError: cannot import name utils

, then modify import line to ,然后修改导入行

requests = eventlet.import_patched('requests.__init__')

from the docs : 来自文档

there are lots of projects out there that combine Requests with one of Python's asynchronicity frameworks. 有很多项目将Requests与Python的异步框架结合起来。 Two excellent examples are grequests and requests-futures. 两个很好的例子是grequestsrequests-futures。

for eventlet specifically you can use erequests . 对于eventlet,您可以使用erequests

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

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