try: result = worker['worker'](worker['args']) if worker['cb'] isnotNone: self.cb_lock.acquire() worker['cb'](result) self.cb_lock.release() except Exception as err: print(err)
defpool_start(self): for thread in self.threads: thread.start()
defpool_join(self): for thread in self.threads: thread.join()
if __name__ == '__main__': deftask(num): count = 0 for i inrange(num): count += i + 1 return num, count
defcb(result): return # print(result)
tp = ThreadPool(100) for i inrange(1000): tp.add_worker(task, i, cb) tp.pool_start() tp.pool_join()