生成函数的并行进程,并将几个不同的参数传递给函数 [英] Spawn parallel processes for function and pass several different arguments to function

查看:75
本文介绍了生成函数的并行进程,并将几个不同的参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我以 Jiaaro的解决方案作为模板将其从线程转换为多处理:

Hi everybody I took Jiaaro's solution as a template to convert it from threading to multiprocessing:

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))

It works nicely, but now I want to pass a second argument that is not a list to the run function, like p = multiprocessing.Process(target=run,args=([vitems.pop()],second_arg))

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

This causes the function to break, because if I do so, the full list of vitems is passed to the function in every single process.

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

How can i still only pass one item of vitems per process to the function AND also pass a second non-list argument to the function run ?

提前致以最诚挚的问候!

thanks in advance and best regards!

推荐答案

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

p = multiprocessing.Process(target=run, args=[vitems.pop()]) does not pass a list to run, it passes the item poped from vitems as the first argument. args is supposed to be a list/tuple of arguments passed to run.

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

To pass multiple arguments just do:

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

这篇关于生成函数的并行进程,并将几个不同的参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆