A
AdSR
Hello,
Nothing relevant found on Google for this, so I'm asking here. I wrote
a short script that launches several threads (parallel download using
urllib.urlretrieve in this case). Sometimes when I launch my script
under Cygwin, I get strange error messages like:
2 [win] python 1912 Winmain: Cannot register window class
Sometimes all of the threads run OK, otherwise after the "correct"
ones finish the console stops responding anyway. No problems directly
under Windows, and /lib/python2.3/test/test_threading.py works fine
too. What is wrong?
Here is the script:
---- wp_retr.py ----
#!/usr/bin/env python
"""Retrieve files from the list, give new names if present in list."""
from __future__ import division
import re
import urllib
import threading
import time
import sys
class Download:
def __init__(self, url, name):
self.url = url
self.name = name
self.last_check = 0.0
class DownloadBag:
def __init__(self, downloads):
self._downloads = []
for url, name in downloads:
self._downloads.append(Download(url, name))
def start(self):
threads = []
for d in self._downloads:
p = self._createProgress(d)
thargs = (d.url, d.name, p)
th = threading.Thread(target=urllib.urlretrieve,
args=thargs)
threads.append(th)
th.start()
return threads
def _progress(self, download, blocks, blk_size, total):
t = time.time()
if t - download.last_check > 5:
if total != -1:
print "%s -> %s: %2.1f%%" % (download.url,
download.name,
blocks*blk_size/total*100)
else:
print "%s -> %s: %d bytes" % (download.url,
download.name,
blocks*blk_size)
download.last_check = t
def _createProgress(self, download):
def progf(blocks, blk_size, total):
self._progress(download, blocks, blk_size, total)
return progf
if __name__ == "__main__":
spcre = re.compile(r"(.*?)\s+(.*)$")
slashre = re.compile(r"(?:.*)/(.*?)$")
f = file(sys.argv[1]) #"filelist.txt")
filelist = []
for line in f:
line = line.strip()
m = spcre.match(line)
if m:
url, name = m.groups()
else:
m = slashre.match(line)
if m:
url, name = line, m.group(1)
else:
print "Incorrect line format:\n%s" % line
continue
filelist.append((url, name))
f.close()
db = DownloadBag(filelist)
threads = db.start()
for t in threads:
t.join()
----
and here is an example input file (run as python filelist.py
filelist.txt):
---- filelist.txt ----
http://www.gnu.org/software/bash/manual/bashref.html
http://www.gnu.org/software/make/manual/html_mono/make.html
Nothing relevant found on Google for this, so I'm asking here. I wrote
a short script that launches several threads (parallel download using
urllib.urlretrieve in this case). Sometimes when I launch my script
under Cygwin, I get strange error messages like:
2 [win] python 1912 Winmain: Cannot register window class
Sometimes all of the threads run OK, otherwise after the "correct"
ones finish the console stops responding anyway. No problems directly
under Windows, and /lib/python2.3/test/test_threading.py works fine
too. What is wrong?
Here is the script:
---- wp_retr.py ----
#!/usr/bin/env python
"""Retrieve files from the list, give new names if present in list."""
from __future__ import division
import re
import urllib
import threading
import time
import sys
class Download:
def __init__(self, url, name):
self.url = url
self.name = name
self.last_check = 0.0
class DownloadBag:
def __init__(self, downloads):
self._downloads = []
for url, name in downloads:
self._downloads.append(Download(url, name))
def start(self):
threads = []
for d in self._downloads:
p = self._createProgress(d)
thargs = (d.url, d.name, p)
th = threading.Thread(target=urllib.urlretrieve,
args=thargs)
threads.append(th)
th.start()
return threads
def _progress(self, download, blocks, blk_size, total):
t = time.time()
if t - download.last_check > 5:
if total != -1:
print "%s -> %s: %2.1f%%" % (download.url,
download.name,
blocks*blk_size/total*100)
else:
print "%s -> %s: %d bytes" % (download.url,
download.name,
blocks*blk_size)
download.last_check = t
def _createProgress(self, download):
def progf(blocks, blk_size, total):
self._progress(download, blocks, blk_size, total)
return progf
if __name__ == "__main__":
spcre = re.compile(r"(.*?)\s+(.*)$")
slashre = re.compile(r"(?:.*)/(.*?)$")
f = file(sys.argv[1]) #"filelist.txt")
filelist = []
for line in f:
line = line.strip()
m = spcre.match(line)
if m:
url, name = m.groups()
else:
m = slashre.match(line)
if m:
url, name = line, m.group(1)
else:
print "Incorrect line format:\n%s" % line
continue
filelist.append((url, name))
f.close()
db = DownloadBag(filelist)
threads = db.start()
for t in threads:
t.join()
----
and here is an example input file (run as python filelist.py
filelist.txt):
---- filelist.txt ----
http://www.gnu.org/software/bash/manual/bashref.html
http://www.gnu.org/software/make/manual/html_mono/make.html