Multiple urllib2 connections

对着背影说爱祢 提交于 2019-12-12 19:13:19

问题


I want to download multiple images at the same time. For that I'm using threads, each one downloading an image, using urllib2 module. My problem is that even if threads starts (almost) simultaneously, the images are downloaded one by one, like in a single-threaded environment.

Here is the threaded function:

def updateIcon(self, iter, imageurl):
    req = urllib2.Request('http://site.com/' + imageurl)
    response = urllib2.urlopen(req)
    imgdata = response.read()
    gobject.idle_add(self.setIcon, iter, imgdata)

Debugging my code I found that downloads seems to get stuck at "response = urllib2.urlopen(req)" line. What's the problem? It's because the threading module or urllib2? How I can fix that?

Thank you in advance


回答1:


Consider using urllib3. It supports connection pooling and multiple concurrent requests via processes (not threads). It should solve this problem. Be careful to garbage collect connection pools if you contact many different sites, since each site gets its own pool.




回答2:


In my experience, multithreads of CPython seems to make better performance than those of sigle thread. Because CPython has thread implementation based on kernel thread. But the difference is little, because of GIL(Global Interpreter Lock). Substitute multiprocessing for multithreading. It's easy. Both have similar interface.



来源:https://stackoverflow.com/questions/4139988/multiple-urllib2-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!