python gevent asyncio_python用from gevent import monkey; monkey.patch_all()之后报ssl等错误
樓主今天第一次用python基于greenlet實現的第三方協程庫gevent,由于gevent在切換IO操作(文件IO、網絡IO)時是自動完成的,所以gevent需要通過修改Python自帶的一些阻塞式系統調用的標準庫,包括socket、ssl、threading和 select等模塊,而變為協程,這一過程需要在啟動時通過monkey patch完成。
importgeventfrom gevent importmonkey
monkey.patch_all()
樓主遇到的報錯如下(簡略版,只保留了前半部分報錯內容):
Traceback (most recent call last):
File"/usr/local/python3.6/lib/python3.6/site-packages/gevent/greenlet.py", line 536, inrun
result= self._run(*self.args, **self.kwargs)
File"test.py", line 14, inreq
res=requests.get(url)
File"/usr/local/python3.6/lib/python3.6/site-packages/requests/api.py", line 72, ingetreturn request(‘get‘, url, params=params, **kwargs)
File"/usr/local/python3.6/lib/python3.6/ssl.py", line 459, inoptions
super(SSLContext, SSLContext).options.set(self, value)
[Previous line repeated316 more times]
解決方案:
仔細閱讀官方文檔發現有這樣一段Tip:
Tip:
When monkey patching, itis recommended to do so as early as possible inthe lifetime of the process.
If possible, monkey patching should be the first lines executed.
Monkey patching later, especiallyif native threads have been created, atexit or signal handlers have been installed, or sockets have been created,
may lead to unpredictable results including unexpected LoopExit errors.
即,monkey?patching需要放到第一行導入,否則會報錯,所以,把?from gevent import monkey;monkey.patch_all() 放到文件最前面就好啦
注:
2、如果你用的python3.6,推薦使用asyncio
總結
以上是生活随笔為你收集整理的python gevent asyncio_python用from gevent import monkey; monkey.patch_all()之后报ssl等错误的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java从零基础到精通教程全套视频课程
- 下一篇: qt5 tcp服务器编程 多固定客户_如