import threading import time import random deftakeSleep(id, name): print(name+'-'+id+':线程开始运行...') time.sleep(random.randint(0, 3)) print(name+'-'+id+':线程任务结束') print('主程序开始运行...') threads = [] for i inrange(0, 5): t = threading.Thread(target=takeSleep, args=(str(i), 'zhangphil')) threads.append(t) t.start() print('主程序运行中...') #等待所有线程任务结束。 for t in threads: t.join() print("所有线程任务完成")
if __name__ == '__main__': process_list = [] for i inrange(5): #开启5个子进程执行fun1函数 p = Process(target=fun1,args=('Python',)) #实例化进程对象 p.start() process_list.append(p)
deffunc1(): for i inrange(0, 100000000): pass return'abc' deffunc2(): for i inrange(0, 100000000): pass return'xyz' if __name__=='__main__': p1 = _pool.submit(func1) p2 = _pool.submit(func2)