事实上,多进程、多线程、进程线程池、协程都可以实现并发
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor from threading import currentThread import os,time,random def task(name): # print('name:%s pid:%s run'%(name,os.getpid())) print('name:%s pid:%s run' % (currentThread().getName(), os.getpid())) time.sleep(random.randint(1,3)) if __name__ == '__main__': # pool = ProcessPoolExecutor(4) # 但只有4个进程执行,同一时间只有4个任务在执行,走一个任务,空下来的进程可以执行其他任务 pool = ThreadPoolExecutor(5) for i in range(10): pool.submit(task,'egon%s'%i) # 会把10个任务全扔到池子里,此时并未创建进程,无进程号 pool.shutdown(wait=True) print('主')