AttributeError: module 'select' has no attribute 'error'解决方法
gevent 實現多協程的時候,出現了上面的錯誤
錯誤的代碼如下:
import requests from gevent import monkey monkey.patch_all() import gevent def f(url):print('GET: %s' % url)data = requests.get(url).textprint('%d bytes received from %s.' % (len(data), url))gevent.joinall([gevent.spawn(f, 'https://www.python.org/'),gevent.spawn(f, 'https://www.yahoo.com/'),gevent.spawn(f, 'https://github.com/'), ])比較糾結,因為在網上大家似乎都是這么寫的呀?為什么我會報錯了??
查了半天也沒查到有關的信息, 后來我在github上看到一篇講gevent - ‘module’ object has no attribute ‘epoll’的時候,雖然也沒有看到些上面,但是看到評論區中有人說了這樣的話
I agree that monkey patching has to be done before everything. I have a django app running with uwsgi+supervisord config. And gevent.monkey.patch_all() was the first line of wsgi.py (which is the entry point to the app). But still this exception was thrown. So either adding –gevent-early-monkey-patch in the uwsgi config or gevent.monkey.patch_all(select=False) in the wsgi.py fixed the error. I’m not sure that this is relevant to the requests or urllib3 libs but just for someone’s information.
看到最后那句話的時候,突然靈光一閃!這不是剛好解釋了我的問題了么?
因為我的報錯正是關于select的呀!雖然我寫的代碼沒有用到那個config,但是我也是在這里遇到了問題了呀!
AttributeError: module ‘select’ has no attribute ‘error’
所以正確的解法應該是:
import requests from gevent import monkey monkey.patch_all(select=False) import gevent def f(url):print('GET: %s' % url)data = requests.get(url).textprint('%d bytes received from %s.' % (len(data), url))gevent.joinall([gevent.spawn(f, 'https://www.python.org/'),gevent.spawn(f, 'https://www.yahoo.com/'),gevent.spawn(f, 'https://github.com/'), ])然后,我們就可以很愉快的開始我們的表演了:請看下面的gif
嘿嘿嘿~ 這樣我們在不同的協程中,只要遇到了IO或者網絡上的問題,就直接切換了~
Hello gevent~
最后,老套路,宣傳一波自己的公眾號!(求關注哇!)
本人中大一肥宅,歡迎大家關注,請掃下面的二維碼(〃’▽’〃)
如果覺得有幫助的話,可以掃碼,贊賞鼓勵一下!謝謝!
總結
以上是生活随笔為你收集整理的AttributeError: module 'select' has no attribute 'error'解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python并发Gevent库(一)
- 下一篇: gevent.joinall()开启协程