在使用多进程加协程时,patch_all()后出现警告信息:MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to errors, including RecursionError。有时会导致不能正常使用requests或者其他包,其实只要调整import的顺序即可,把
import gevent,
from gevent import monkey,
monkey.patch_all()三行语句放在其他所有的import语句之前,可以避免出现警告或者报错信息,导致程序不能正常运行。详情见linux下的代码:
don16@don16-VirtualBox:~$ python感谢gevent包作者在github的回答,感兴趣的可以去阅读原帖https://github.com/gevent/gevent/issues/1016查看原因,建议只要使用gevent记得把gevent相关的import语句放在所有其他import语句的前边。
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> import gevent
>>> from gevent import monkey
>>> monkey.patch_all()
__main__:1: MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to errors, including RecursionError on Python 3.6. Please monkey-patch earlier. See https://github.com/gevent/gevent/issues/1016
True
>>> quit()
don16@don16-VirtualBox:~$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gevent
>>> from gevent import monkey
>>> monkey.patch_all()
True
>>> import requests
>>>