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

原文:https://www.cnblogs.com/hcy-fly/p/10268238.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值