带有布尔值和多个参数的python多处理 [英] python multiprocessing with boolean and multiple arguments

查看:57
本文介绍了带有布尔值和多个参数的python多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受多个参数的函数,其中一些是布尔值.我正在尝试将其传递给多处理 pool.apply_async 并希望传递一些附加名称的参数.

I have a function that takes multiple arguments, some of which are boolean. I'm trying to pass this to the multiprocessing pool.apply_async and want to pass some args with the names attached.

这是我正在使用的示例脚本:

Here's an example script that I'm working with:

from multiprocessing import Pool

    def testFunc(y, x, calcY=True):
        if calcY == True:
            return y*y
        elif calcY == False:
            return x*x

    if __name__ == "__main__":
        p = Pool()
        res = p.apply_async(testFunc, args = (2, 4, False))
        print res.get()

这有效,但我很好奇将 res = p.apply_async(testFunc, args = (2, 4, False)) 更改为类似:

This works, but I'm curious about changing the res = p.apply_async(testFunc, args = (2, 4, False)) to something like:

res = p.apply_async(testFunc, args = (2, 4, calcY = False))

推荐答案

apply_asyncargskwds 关键字参数,你可以这样使用:

apply_async has args and kwds keyword arguments which you could use like this:

res = p.apply_async(testFunc, args=(2, 4), kwds={'calcY': False})

这篇关于带有布尔值和多个参数的python多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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