Unhandled exception in thread

Scott David Daniels Scott.Daniels at Acm.Org
Thu Jun 11 17:34:27 EDT 2009


Mads Michelsen wrote:
> ...I'm getting some error reports ... looking for advice on a way that
> I can investigate the error.... The script ... downloads and...
 > in the background using a separate thread (the 'thread' module), ....
 > Sometimes, .... when I quit the script, the following error message 
is printed:
> 
>     Unhandled exception in thread started by
>     Error in sys.excepthook:
> 
>     Original exception was:
> 
> And that's it! Seems like there's some information missing? What is
> the error in sys.excepthook? What was the original exception? And
> where?
> 
> Any suggestions on how to proceed?
(1) Generally, use Threading rather than thread.  It says so at the top
     of the thread docs.
(2) Sounds like it might be a timing problem while shutting down.
     Do you tell the thread to stop, and then stop it?  That would
     generally be the best architecture.
(3) From above, what you might want is something like:

         import Threading
         import Queue

         ...
         def code(commands, replies):
             for element in iter(commands.get, None): # None: stop signal
                 time.sleep(1) # just here to represent processing time.
                 results = repr(element) # and simulating some results"
                 replies.put(results)
             replies.put(None) # superfluous indication of end
         ...
         def main():
             ...
             commands = queue.Queue()
             replies = queue.Queue()

             worker = threading.Thread(target=code,
                                       args=(commands, replies))
             worker.daemon = True # or use .setDaemon(True) for older

             for n in range(5):
                 commands.put(n)
             print 'first couple:', results.get(), results.get()

             commands.put(None) # tell the worker to stop.
             worker.join() # wait for the worker to shut down.

Making the worker a daemon, telling it to stop with a command, and
waiting for it to finish with a .join are all belts-and-suspenders
related to the thread shutdown, but it sounds like you have a thread
shutdown issue.

--Scott David Daniels
Scott.Daniels#Acm.Org



More information about the Python-list mailing list