python协程使用场景_python中socket、进程、线程、协程、池的创建方式和应用场景...

七、进程池1、同步提交apply:importosimporttimefrom multiprocessing importPooldeftest(num):

time.sleep(1)print('%s:%s' %(num,os.getpid()))return num*2

if __name__ == '__main__':

p=Pool()for i in range(20):

res= p.apply(test,args=(i,)) #提交任务的方法 同步提交

print('-->',res) #res就是test的return的值,同步提交的返回值可以直接使用

2、异步提交apply_async:2-1无返回值:importtimefrom multiprocessing importPooldeffunc(num):

time.sleep(1)print('做了%s件衣服'%num)if __name__ == '__main__':

p= Pool(4) #进程池中创建4个进程,不写的话,默认值为你电脑的CUP数量

for i in range(50):

p.apply_async(func,args=(i,)) #异步提交func到一个子进程中执行,没有返回值的情况

p.close() #关闭进程池,用户不能再向这个池中提交任务了

p.join() #阻塞,直到进程池中所有的任务都被执行完

2-2有返回值:importtimeimportosfrom multiprocessing importPooldeftest(num):

time.sleep(1)print('%s:%s' %(num,os.getpid()))return num*2

if __name__ == '__main__':

p=Pool()

res_lst=[]for i in range(20):

res= p.apply_async(test,args=(i,)) #提交任务的方法 异步提交

res_lst.append(res)for res inres_lst:print(res.get()) #异步提交的返回值需要get,get有阻塞效果,此时就不需要close和join

2-3map:

map接收一个函数和一个可迭代对象,是异步提交的简化版本,自带close和join方法

可迭代对象的每一个值就是函数接收的实参,可迭代对象的长度就是创建的任务数量

map可以直接拿到返回值的可迭代对象(列表),循环就可以获取返回值importtimefrom multiprocessing importPooldeffunc(num):print('子进程:',num)#time.sleep(1)

returnnumif __name__ == '__main__':

p=Pool()

ret= p.map(func,range(10)) #ret是列表

for i inret:print('返回值:',i)2-4回调函数:importosfrom multiprocessing importPooldeffunc(i):print('子进程:',os.getpid())returnidefcall_back(res):print('回调函数:',os.getpid())print('res--->',res)if __name__ == '__main__':

p=Pool()print('主进程:',os.getpid())

p.apply_async(func,args=(1,),callback=call_back) #callback关键字传参,参数是回调函数

p.close()

p.join()

八、进程池、线程池

线程池:1、importtimefrom concurrent.futures importThreadPoolExecutordeffunc(i):print('thread',i)

time.sleep(1)print('thread %s end'%i)

tp= ThreadPoolExecutor(5) #相当于tp = Pool(5)

tp.submit(func,1) #相当于tp.apply_async(func,args=(1,))

tp.shutdown() #相当于tp.close() + tp.join()

print('主线程')2、importtimefrom concurrent.futures importThreadPoolExecutorfrom threading importcurrentThreaddeffunc(i):print('thread',i,currentThread().ident)

time.sleep(1)print('thread %s end'%i)

tp= ThreadPoolExecutor(5)for i in range(20):

tp.submit(func,i)

tp.shutdown()#shutdown一次就够了,会自动把所有的线程都join()

print('主线程')3、返回值importtimefrom concurrent.futures importThreadPoolExecutorfrom threading importcurrentThreaddeffunc(i):print('thread',i,currentThread().ident)

time.sleep(1)print('thread %s end' %i)return i * '*'tp= ThreadPoolExecutor(5)

ret_lst=[]for i in range(20):

ret=tp.submit(func,i)

ret_lst.append(ret)for ret inret_lst:print(ret.result()) #相当于ret.get()

print('主线程')4、map

map接收一个函数和一个可迭代对象

可迭代对象的每一个值就是函数接收的实参,可迭代对象的长度就是创建的线程数量

map可以直接拿到返回值的可迭代对象(列表),循环就可以获取返回值importtimefrom concurrent.futures importThreadPoolExecutordeffunc(i):print('thread',i)

time.sleep(1)print('thread %s end'%i)return i * '*'tp= ThreadPoolExecutor(5)

ret= tp.map(func,range(20))for i inret:print(i)5、回调函数

回调函数在进程池是由主进程实现的

回调函数在线程池是由子线程实现的importtimefrom concurrent.futures importThreadPoolExecutorfrom threading importcurrentThreaddeffunc(i):print('thread',i,currentThread().ident)

time.sleep(1)print('thread %s end'%i)return i * '*'

defcall_back(arg):print('call back :',currentThread().ident)print('ret :',arg.result()) #multiprocessing的Pool回调函数中的参数不需要get(),这里需要result()

tp= ThreadPoolExecutor(5)

ret_lst=[]for i in range(20):

tp.submit(func,i).add_done_callback(call_back)#使用add_done_callback()方法实现回调函数

print('主线程',currentThread().ident)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值