封装代码 class MyThread(object):def __init__(self):# 线程池 根据自己需要传入最大线程数量,我只需要一个所以传1self.executor = ThreadPoolExecutor(2)# 用于存储期程self.future_dict = {}# 检查worker线程是否正在运行def is_running(self, tag):future = self.future_dict.get(tag, None)if future and future.running():return Truereturn Falsedef __del__(self):self.executor.shutdown()thread = MyThread()
使用实例
if not thread.is_running("worker"):future = thread.executor.submit(添加需要异步的函数,x,y,z)thread.future_dict["worker"] = future