0

I'm doing a large download from which any number of exceptions can be raised. I have code that seems to work but not certain it will work in all cases.

I have my basic structure below that catches an error in an overloaded thread class then sets a flag in that class and lets whatever remaining queue items to cycle out with no action. The already running threads should not be interrupted.

Is there a potential bug in the structure below or can it be improved?

Previously asked question: How to handle exception in threading with queue in Python?

class DownloadWorker(Thread):
    my_error_message = None  # Critical Error Flag
    def __init__(self, queue):
        Thread.__init__(self)
        self.queue = queue

    def run(self):
        while True:
            thread_name = self.queue.get()
            if DownloadWorker.my_error_message:  
                self.queue.task_done()  # One by one, this will terminate remaining items in queue
            else:
                try:
                    self.dummy_download_data(thread_name)  # Process something here
                except Exception as e:
                    DownloadWorker.my_error_message = "Fatal Error in " + str(e)  # omitted lock/release
                finally:
                    self.queue.task_done()

    def dummy_download_data(self, thread_name):
        if thread_name == 'Thread2':  # mimic some error in some function way deep down
            raise Exception("? My Problem with " + thread_name)
        else:
            time.sleep(2)

main()
    thread_list = ['Thread1', 'Thread2', 'Thread3', 'Thread4', 'Thread5']
    queue = Queue()
    for x in range(3): 
        worker = DownloadWorker(queue)  
        worker.daemon = True  
        worker.start()  

    for name in thread_list: # Better way to handle exceptions thown in a thread?
        queue.put(name)

    queue.join()
    if DownloadWorker.my_error_message:  # Handling my error
        print(DownloadWorker.my_error_message)
    print('fini')
2
  • "I have code that seems to work but not certain it will work in all cases." Then run all imaginable test cases, and IF a problem arises, you can post it and we may be able to help you. Commented Jun 15, 2018 at 21:11
  • #Gabriel. What I'm really seeking is if there is a known pythonic way to process exceptions in a thread model. Commented Jun 15, 2018 at 22:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.