python 处理多线程返回值

2018-11-23  本文已影响0人  青铜搬砖工
import threadpool


def f(index):
    print("{} in".format(index))
    sum = 0
    for i in range(10):
        sum+=i
    return "process {} sum {} ".format(index,sum)


def huidiao(workWorkRequest,s):
    print(workWorkRequest)
    print("output {}".format(s))

def main():
    pool = threadpool.ThreadPool(3)
    requests = threadpool.makeRequests(f, [11,12,13,14,15,16,17,18,19], huidiao)
    [pool.putRequest(req) for req in requests]
    pool.wait()

if __name__ == "__main__":
    main()

python线程池有一个回调函数,可以通过回调函数处理线程返回值,线程池的回调函数默认有一个 workWorkRequest的参数,作用如下


image.png
上一篇下一篇

猜你喜欢

热点阅读