微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python,gevent,urllib2.urlopen.read,下载加速器

我正在尝试为Linux构build一个下载加速器。 我的程序使用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",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>,7,-1)Failed with AssertionError

我的程序通过调用以下方法从URL获取文件字节大小:

urllib2.urlopen(URL).info().get("Content-Length")

并用除数除文件大小,从而将下载过程分解成多个部分。 在这个例子中,我打破了下载到10个部分。

如何在windows上安装zeroRPC(python)

如何运行Nginx,gevent,virtualenv和django

为什么gevent在Apache + mod_wsgi的Flask应用程序中引发NotImplementedError?

Windows上的gevent TCP服务器

Nginx中禁用请求缓冲

每个greenlet在这个故事中运行一个命令:

urllib2.urlopen(URL).read(offset)

这里是一个链接到我的代码托pipe在饼图 : http : //pastie.org/3253705

感谢您的帮助!

仅供参考:我在Ubuntu 11.10上运行。

理解Apache平台每秒的请求

如何将远程IP传递给代理服务? – Nginx

如何解决这个“NotImplementedError”在Windows上的urllib2 / gevent错误

一起使用gevent和multiprocessing与subprocess通信

如何用Python编写Web应用程序?

您正尝试读取来自不同greenlet的单个请求的响应。

如果你想使用几个并发连接下载相同的文件,那么你可以使用Range http header(如果服务器支持它)(对于带有Range标头的请求,你得到了206的状态而不是200)。 请参阅HTTPRangeHandler 。

read的参数是多个字节,而不是偏移量。

看来gevent会让你异步调用urllib,但是不能让你从多个greenlet访问相同的资源。

此外,由于它正在使用wait_read,效果仍然是从文件同步,顺序读取(与您想要实现的完全相反)。

我建议你可能需要比urllib2低一些,或者使用一个不同的库。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐