简体   繁体   中英

In python, how can a main thread catch the exception from a background thread?

I'm playing around with a simple thread program found in this post: http://pymotw.com/2/Queue/#using-queues-with-threads

The program runs OK, but if modify the worker thread to raise an exception, the main thread doesn't catch it and terminate properly. How do I catch the exception from main thread and terminate the program gracefully?

It is not possible to catch an exception raised on a different thread. You will have to catch the exception on the thread that causes it, and then send a message of some kind to the main thread. You can of course include the actual exception object in the message you send.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM