天天看点

进程池与列表循环多参传递

版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/77573585

使用列表循环的示例
#coding: utf-8
import multiprocessing
import time

def func(msg, msg2):
    print "msg:", msg
    time.sleep(3)
    print "end"
    print msg2

list = ["test", "hello", "hi", "good", "yes", "well"]


if __name__ == "__main__":
    pool = multiprocessing.Pool(processes = 3)
    for i in list:
        msg = "hello"
        pool.apply_async(func, (msg, i))   #维持执行的进程总数为processes,当一个进程执行完毕后会添加新的进程进去

    print "Mark~ Mark~ Mark~~~~~~~~~~~~~~~~~~~~~~"
    pool.close()
    pool.join()   #调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
    print "Sub-process(es) done."           

继续阅读