python开启多线程
from threading import Thread class MyThread(Thread): def __init__(self, func, args): super(MyThread, self).__init__() self.func = func self.args = args def run(self): self.result = self.func(*self.args) def get_result(self): try: return self.result except Exception: return None def run(i): time.sleep(2) return i if __name__ == '__main__': a = time.time() threads = [] result = [] for i in range(11): task = MyThread(run, args=(i,)) threads.append(task) for single_task in threads: single_task.start() for single_task in threads: single_task.join() for single_task in threads: result.append(single_task.get_result()) for i in result: print(i) print(time.time() - a)