码迷,mamicode.com
首页 > 系统相关 > 详细

进程池的用法

时间:2019-06-01 15:17:07      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:adp   函数   nbsp   class   cut   res   进程池   submit   exe   

这是一个函数,没有返回值和其他的函数交互的方式

from concurrent.futures import ThreadPoolExecutor
import time
pool = ThreadPoolExecutor(50)

def f1():
    print(hello)
    time.sleep(1)


for i in range(200):
    pool.submit(f1)

要是有函数参数的调用

    from concurrent.futures import ThreadPoolExecutor
import time
# import random
pool = ThreadPoolExecutor(50)

def f1(i):
    print(hello)
    time.sleep(1)
    i+=1
    return i

def f2(i):
    i=i.result()#接受到的i是一个对象,所以要result一下
    time.sleep(1)
    print(i)




for i in range(200):
    #此处的i是f1的参数,把f1的返回值用于f2的时候需要使用函数的回调
    pool.submit(f1,i).add_done_callback(f2)

 

进程池的用法

标签:adp   函数   nbsp   class   cut   res   进程池   submit   exe   

原文地址:https://www.cnblogs.com/ouyang99-/p/10959668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!