在 python中运行monkey.patch_all()报错问题

在最新的python3.6中使用协程,启用gevent时,可能会出现警告,代码如下:
import gevent
from gevent import monkey
monkey.patch_all()
# monkey.patch_socket()
# monkey.patch_ssl()
import urllib.request

def run_task(url):
    print("Visit --> %s" % url)
    try:
        response = urllib.request.urlopen(url)
        data = response.read()
        print("%d bytes received from %s" % (len(data), url))
    except Exception as e:

        print("程序运行错误", e)

if __name__ == '__main__':
    urls = ['https://github.com/', 'https://www.python.org/', 'http://www.cnblogs.com/']
    greenlets = [gevent.spawn(run_task, url) for url in urls]
    gevent.joinall(greenlets)

运行结果如下所示:
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
  monkey.patch_all()
Visit --> https://github.com/
Visit --> https://www.python.org/
Visit --> http://www.cnblogs.com/
46365 bytes received from http://www.cnblogs.com/
48801 bytes received from https://www.python.org/
59467 bytes received from https://github.com/

而访问 https://github.com/gevent/gevent/issues/1016后可知,方法是将
 

import gevent
import gevent.monkey
>>> gevent.monkey.patch_all()

置顶,但再次运行后发现依然出现相同的红色警告,这是因为python3.6自带的gevent版本过低所致,卸载后重新安装最新
gevent版本即可。

解决方案:

pip uninstall gevent

pip install gevent

再次运行成功,搞定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值