google tech talk code (threading module)

MRAB python at mrabarnett.plus.com
Tue Sep 8 08:47:17 EDT 2009


wiso wrote:
> I took a little code from google tech talk. It seems interesting, but it 
> doesn't work:
> 
> import sys, urllib, os, threading, Queue
> 
> q = Queue.Queue()
> 
> class RetrWorker(threading.Thread):
>     def run(self):
>         self.setDaemon(True)
>         def hook(*a): print (fn,a)
>         while True:
>             url = q.get()
>             fn = os.path.basename(url)
>             print url, "->", fn
>             urllib.urlretrive(url, fn, hook)
> 
> for i in range(10): RetrWorker().start()
> for url in sys.argv[1:]: q.put(url)
> 
> Exception in thread Thread-10:
> Traceback (most recent call last):
>   File "/usr/lib64/python2.6/threading.py", line 522, in __bootstrap_inner
>     self.run()
>   File "wget_m.py", line 7, in run
>     self.setDaemon(True)
>   File "/usr/lib64/python2.6/threading.py", line 690, in setDaemon
>     self.daemon = daemonic
>   File "/usr/lib64/python2.6/threading.py", line 683, in daemon
>     raise RuntimeError("cannot set daemon status of active thread");
> RuntimeError: cannot set daemon status of active thread
> 
The traceback explains why.

self.setDaemon(True) is in the 'run' method, which is called when the
thread starts (becomes active), but you're not allowed to turn the
thread into a daemon after it has started.



More information about the Python-list mailing list