创建线程池

import  time
from  concurrent.futures  import  ThreadPoolExecutor
import  threading
import  os

def  work(n):
    print(f'给{n}打电话,进程号是:{os.getpid},线程号是:{threading.current_thread()}')
    
    print(f'{n}通话结束,进程号是:{os.getpid},线程号是:{threading.current_thread()}')
    print('\n')
userlist=['刘德华','吴彦祖','梁朝伟','林俊杰']

#创建线程池
pool=ThreadPoolExecutor(max_workers=3)
#循环指定任务
[pool.submit(work,user)  for  user  in  userlist]

#关闭线程池和进程池
pool.shutdown()

猜你喜欢

转载自www.cnblogs.com/luckiness/p/13181214.html