7.2.2.threading
class TheradRunner(): def __init__(self): self.__threadPool = {} def addThreadWorker(self, worker, name): self.__threadPool.update({name:Thread(worker)}) def run(self): for name, thread in self.__threadPool.items(): thread.start() for name, thread in self.__threadPool.items(): thread.join()class Thread(threading.Thread): def __init__(self, workerClass): super().__init__() self.status = Status.Init if hasattr(workerClass, 'run') and callable(workerClass.run): self.__workerClass = workerClass self.exceptionMessage = None else: self.status = Status.Error self.exceptionMessage = 'Worker function error' def start(self): if Status.Init == self.status: self.status = Status.Running super().start() def run(self): try: self.__workerClass.run() self.status = Status.Success except Exception: self.exceptionMessage = traceback.format_exc() self.status = Status.Errorclass Worker(): def run(self): passclass ReadLineWorker(Worker): def __init__(self, aHoldingZoneMap): self.__holdingZoneTableMap = aHoldingZoneMap def run(self): self.readLinesAndAttribute()lineWorker = ReadLineWorker(holdingZoneTableMap) threadRunner.addThreadWorker(lineWorker, 'ReadLineWorker') threadRunner.run()
Last updated
