我在 celery workers
中使用 Python requests
进行大量(~10/秒)API 调用(包括 GET、POST、PUT、DELETE)。每个请求大约需要 5-10 秒才能完成。
我尝试在 eventlet
池中运行 celery worker,并发数为 1000。
由于请求
是阻塞进程,每个并发连接都在等待一个请求。
如何使请求
异步?
最佳答案
使用 eventlet monkey patching使任何纯 Python 库成为非阻塞的。
补丁单库
# import requests # instead do this: import eventlet requests = eventlet.import_patched('requests')
修补一切
import eventlet eventlet.monkey_patch() # must execute as early as possible ... # everything is non-blocking now: import requests, amqp, memcache, paramiko, redis
更新:有known issue与猴子修补请求库。如果你得到:
ImportError: cannot import name utils
,然后修改导入行为
requests = eventlet.import_patched('requests.__init__')
关于python - Celery + Eventlet + 非阻塞请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28315657/