为函数生成并行进程并将几个不同的参数传递给函数

时间:2015-06-05 09:41:25

标签: python multiprocessing python-multiprocessing

大家好我以Jiaaro's solution为模板将其从线程转换为多处理:

import multiprocessing
from function_repo import run
from time import time

vitems = ['02','63','25','0']

num_processes = (multiprocessing.cpu_count()/1)
threads = []

if __name__ == '__main__':
    begin = time()
    print begin
    # run until all the threads are done, and there is no data left
    while threads or vitems:
        if( len(threads) < (num_processes -1) ):

            p = multiprocessing.Process(target=run,args=[vitems.pop()])

            p.start()

            print p, p.is_alive()

            threads.append(p)

        else:

            for thread in threads:

                if not thread.is_alive():

                    threads.remove(thread)
    print  'Hello, finished'
    print  'Took: ',(time()-begin)

它运行良好,但现在我想传递第二个不是列表的参数run函数,如p = multiprocessing.Process(target=run,args=([vitems.pop()],second_arg))

这导致函数中断,因为如果我这样做,vitems的完整列表将在每个进程中传递给函数。

我如何仍然只将每个进程的vitems项传递给函数并将第二个非列表参数传递给函数run

提前感谢和最诚挚的问候!

1 个答案:

答案 0 :(得分:0)

p = multiprocessing.Process(target=run, args=[vitems.pop()])未将列表传递给run,它会将来自vitems的项目作为第一个参数传递。 args应该是传递给run的参数的列表/元组。

要传递多个参数,请执行以下操作:

p = multiprocessing.Process(target=run, args=(vitems.pop(), arg2, ...))