python gevent asyncio_python用from gevent import monkey; monkey.patch_all()之后报ssl等错误
樓主今天第一次用python基于greenlet實(shí)現(xiàn)的第三方協(xié)程庫(kù)gevent,由于gevent在切換IO操作(文件IO、網(wǎng)絡(luò)IO)時(shí)是自動(dòng)完成的,所以gevent需要通過(guò)修改Python自帶的一些阻塞式系統(tǒng)調(diào)用的標(biāo)準(zhǔn)庫(kù),包括socket、ssl、threading和 select等模塊,而變?yōu)閰f(xié)程,這一過(guò)程需要在啟動(dòng)時(shí)通過(guò)monkey patch完成。
importgeventfrom gevent importmonkey
monkey.patch_all()
樓主遇到的報(bào)錯(cuò)如下(簡(jiǎn)略版,只保留了前半部分報(bào)錯(cuò)內(nèi)容):
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]
解決方案:
仔細(xì)閱讀官方文檔發(fā)現(xiàn)有這樣一段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需要放到第一行導(dǎo)入,否則會(huì)報(bào)錯(cuò),所以,把?from gevent import monkey;monkey.patch_all() 放到文件最前面就好啦
注:
2、如果你用的python3.6,推薦使用asyncio
總結(jié)
以上是生活随笔為你收集整理的python gevent asyncio_python用from gevent import monkey; monkey.patch_all()之后报ssl等错误的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java从零基础到精通教程全套视频课程
- 下一篇: python sqlserver 列名_