我正在尝试为Linux构建下载加速器.我的程序使用gevent,os和urllib2.我的程序收到一个URL,并尝试同时下载文件.我所有的代码都是有效的.我唯一的问题是urllib2.urlopen.read()阻止我同时运行.read()函数.
这是抛出我的例外.
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/gevent/greenlet.py", line 405, in run
result = self._run(*self.args, **self.kwargs)
File "gevent_concurrent_downloader.py", line 94, in childTasklet
_tempRead = handle.read(divisor) # Read/Download part
File "/usr/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
File "/usr/lib/python2.7/httplib.py", line 561, in read
s = self.fp.read(amt)
File "/usr/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
File "/usr/lib/pymodules/python2.7/gevent/socket.py", line 407, in recv
wait_read(sock.fileno(), timeout=self.timeout, event=self._read_event)
File "/usr/lib/pymodules/python2.7/gevent/socket.py", line 153, in wait_read
assert event.arg is None, 'This event is already used by another greenlet: %r' % (event.arg, )
AssertionError: This event is already used by another greenlet: (<Greenlet at 0x2304958: childTasklet(<__main__.NewFile object at 0x22c4390>, 4595517, <addinfourl at 37154616 whose fp = <socket._fileob, 459551, 1)>, timeout('timed out',))
<Greenlet at 0x2304ea8: childTasklet(<__main__.NewFile object at 0x22c4390>,4595517, <addinfourl at 37154616 whose fp = <socket._fileob, 7, -1)failed with AssertionError
我的程序通过调用以下方法从URL获取文件字节大小来工作:
urllib2.urlopen(URL).info().get("Content-Length")
然后将文件大小除以除数,从而将下载过程分为多个部分.在此示例中,我将下载分为10部分.
每个greenlet在此界面中都运行一个命令:
urllib2.urlopen(URL).read(offset)
这是指向托管在我的代码上的代码的链接:http://pastie.org/3253705
感谢您的帮助!
仅供参考:我在Ubuntu 11.10上运行.
解决方法:
您正在尝试读取对来自不同greenlets的单个请求的响应.
如果您想使用多个并发连接下载同一文件,则可以在服务器支持的情况下使用Range
http header(对于具有Range标头的请求,状态为206,而不是200).参见HTTPRangeHandler
.