以下是看教程中一个例子:
import gevent.monkey
gevent.monkey.patch_all()
import gevent
import urllib2
def fetch(pid):
response = urllib2.urlopen("http://www.baidu.com")
result = response.read()
print pid
def synchronous():
for i in range(1,10):
fetch(i)
def asynchronous():
threads = []
for i in range(1,10):
threads.append(gevent.spawn(fetch,i))
gevent.joinall(threads)
print 'synchronous:'
synchronous()
print 'Asynchronous:'
asynchronous()
会报如下错误 :
ubuntu@yee:/tmp$ python t3.py
synchronous:
Traceback (most recent call last):
File "t3.py", line 26, in <module>
synchronous()
File "t3.py", line 16, in synchronous
fetch(i)
File "t3.py", line 9, in fetch
response = urllib2.urlopen("http://www.baidu.com")
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 66] unknown>
如果注释掉synchonrous函数 ,则报如下的错误 :
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gevent-0.13.8-py2.7-linux-x86_64.egg/gevent/greenlet.py", line 390, in run
result = self._run(*self.args, **self.kwargs)
File "t3.py", line 9, in fetch
response = urllib2.urlopen("http://www.baidu.com")
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
raise URLError(err)
URLError: <urlopen error [Errno 66] unknown>
<Greenlet at 0x1b02e10: fetch(9)> failed with URLError
google了好久,完全没有找到解决方法啊,问别人,别人都说跑在他们的环境上没有问题,我也不知道我的环境有什么问题
>>> import gevent
>>> gevent.__version__
'0.13.8'
Best match: greenlet 0.4.1
greenlet 0.4.1 is already the active version in easy-install.pth
_(:з」∠)_ 到底为什么呀????
v2ex 好歹是程序员的圈子,居然不能贴代码啊,这是什么逻辑?代码一上来全乱了