import threading import time from concurrent.futures import ThreadPoolExecutor, wait import fire as fire def each_task(index): time.sleep(1) #睡眠1s 模拟IO print('thread %s square %d' % (threading.current_thread().ident, index)) return index * index def run(thread_num, task_num): # 实例化线程池,threa_num个线程 executor = ThreadPoolExecutor(thread_num, '') start = time.time() fs = [] for i in range(task_num): fs.append(each_task(i)) # wait(fs) # 等待计算结束 end = time.time() duration = end - start s = sum([f for f in fs]) # 求和 print('total result = %s cost: %.2fs' % (s, duration)) executor.shutdown() # 销毁线程池 if __name__ == '__main__': press = int(input('请输入进程数:')) thread = int(input('请输入线程数:')) fire.Fire(run(press, thread))
线程与进程-运行时间算法1
最新推荐文章于 2021-09-22 15:41:18 发布