warezsoftwarez
warezsoftwarez

Reputation: 57

How to check if thread (from bunch of threads) threw an exception?

I created a script that runs several threads. Each thread runs a function in infinite loop. I would like to know if any of threads threw an exception.

Here is the basic structure how threads are started:

def main():
    VODP = Packager()
    WINSCP = WinSCP()
    threadList = []
    threadList.append(threading.Thread(target=VODP.scan))
    threadList.append(threading.Thread(target=WINSCP.ftp_sender))
    threadList.append(threading.Thread(target=WINSCP.ftp_status_checker))
    threadList.append(threading.Thread(target=WINSCP.synchronize))
    for thread in threadList:
        thread.start()
    for thread in threadList:
        thread.join()

main()

and here is a sample of a class with function in infinite loop:

class Packager:
    def __init__(self):
        self.rootDir, self.scanTime, self.extendedScanTime = self.configure_packager()
        self.foldersReadyToPack = []
    def scan(self):
        while True:
            if self.foldersReadyToPack == []:
                for dirpath, dirnames, filenames in os.walk(self.rootDir):
                    if(folder_verifier(dirpath)):
                        #print("Currently scanned folder: ", dirpath)
                        package_creator(dirpath)
                if len(self.foldersReadyToPack) > 0:
                    #print(f'Folders with suitable files has been found!')
                    packingQueue = PackingQueue(self.foldersReadyToPack)
                    packingQueue.execute_packing()
                    self.foldersReadyToPack = packingQueue.elements

How can I achieve that? How can get the information that thread has threw an exception and is no longer performing its tasks in infinite loop? I would like to get such information to pass it to class responsible for sending emails, when something suspicious occurs in program.

Upvotes: 1

Views: 179

Answers (1)

alex_noname
alex_noname

Reputation: 32123

You could use concurrent.futures:

import concurrent.futures


def foo_with_exc(a, b):
    raise ValueError()


def thread_starter():
    with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
        future = executor.submit(foo_with_exc, 1, 2)

        # Raise any exceptions raised by thread
        return future.result()


try:
    thread_starter()
except ValueError as e:
    print("Exception!")

Upvotes: 1

Related Questions