PDA

View Full Version : Urllib2 without high cpu usage?



NoBugs!
February 10th, 2010, 04:39 AM
I'm using urllib2 to download files, but there seems to be a bug in the read() function. It uses way too much cpu, even when I use it in a while loop such as:



next = a.read(1000)
total+=next
while (len(next)>0):
next = a.read(1000)
total += next

A sleep() in the while loop helps, but slows the download.

Apparently it is a bug in python (http://bugs.python.org/issue1411097), is there any way to get around this?

wmcbrine
February 10th, 2010, 04:44 AM
It's not clear to me that the bug you link to is related to the problem you describe.

I suspect you'll see better performance if you increase the size of the chunk you read. Like, 1000000 instead of 1000.

NoBugs!
February 10th, 2010, 04:16 PM
Yeah, after further testing it seems urllib2 isn't the problem, it's something else. Maybe the downloader should be in a thread.

wmcbrine
February 10th, 2010, 10:05 PM
Maybe the downloader should be in a thread.I can't see how that would help.

Did you try increasing the block size?