python gevent 报:urlopen error 错误,求救啊!!

2013-06-25 15:52:49 +08:00
 yeelone
以下是看教程中一个例子:

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 好歹是程序员的圈子,居然不能贴代码啊,这是什么逻辑?代码一上来全乱了
4708 次点击
所在节点    Python
2 条回复
minggl
2013-06-26 20:45:53 +08:00
1.gevent.monkey.patch_all(dns=false)试试
2.换个gevent版本
yeelone
2013-06-28 14:06:33 +08:00
@minggl 我把版本换到了 1.0dev 就没有报错了。ths

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/73573

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX