nameko使用gevent问题

问题

最近写代码时候为了提高速度,使用gevent改写之前代码,但是引入nameko的时候出现了问题。

翻阅了下官方文档

1
2
3
4
5
6
7
8
9
10
Concurrency
Nameko is built on top of the eventlet library, which provides concurrency via “greenthreads”. The concurrency model is co-routines with implicit yielding.
Implicit yielding relies on monkey patching the standard library, to trigger a yield when a thread waits on I/O. If you host services with nameko run on the command line, Nameko will apply the monkey patch for you.
Each worker executes in its own greenthread. The maximum number of concurrent workers can be tweaked based on the amount of time each worker will spend waiting on I/O.
Workers are stateless so are inherently thread safe, but dependencies should ensure they are unique per worker or otherwise safe to be accessed concurrently by multiple workers.
Note that many C-extensions that are using sockets and that would normally be considered thread-safe may not work with greenthreads. Among them are librabbitmq, MySQLdb and others.

nameko使用了eventlet,当我使用gevent的时候与他的monkey patching引发了冲突。

解决方案

修改gevent为eventlet形式,然后对requests请求打上补丁。

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