简体   繁体   English

Python:与多处理混淆

[英]Python: Confused with multi-processing

I Mostly would like some clarification or explanation... the below script will start 4 different instances of the "pwd_find" function... if the arguments names of the "pwd_find" match that of "break_points" dictionary keys (start, stop, pause)... "pwd_find" function will run normally, HOWEVER, when i changed the last argument (or any) name of "pwd_find" to not match the ones of the dictionary, the whole function will not run... anyone can explain why this happens?我主要想要一些澄清或解释......下面的脚本将启动“pwd_find”function 的 4 个不同实例......如果“pwd_find”的 arguments 名称与“break_points”字典键的名称匹配(开始,停止,暂停)...“pwd_find”function 将正常运行,但是,当我将“pwd_find”的最后一个参数(或任何)名称更改为与字典的名称不匹配时,整个 function 将无法运行...任何人都可以解释为什么会这样? as i thought that parameter name do not matter... ?因为我认为参数名称无关紧要......?

cores = 4
wordlist = []


def pwd_find(start, stop, pa):
    print (pa)

for pw in passwordList:
    wordlist.append(pw)

if __name__ == '__main__':
    break_points = []
    for i in range(cores):
        break_points.append({"start":math.ceil(len(wordlist)/cores * i), "stop":math.ceil(len(wordlist)/cores * (i + 1)), "pause": 123})

    p = Pool(cores)

    for i in break_points:
        a = p.apply_async(pwd_find, kwds=i, args=tuple())

    p.close()
    p.join()```

This is because you're passing all the arguments as keyword-only arguments via the kwds=i parameter here:这是因为您通过此处的kwds=i参数将所有 arguments 作为仅关键字 arguments 传递:

p.apply_async(pwd_find, kwds=i, args=tuple())

Where i is that dictionary {"start": ..., "stop": ..., ...} . i在哪里字典{"start": ..., "stop": ..., ...}

The names of parameters do matter when you're referring to them by name:当您按名称引用参数时,参数的名称确实很重要:

print("Hello", "world", sep=", ")

Here one such argument must be called sep , and if you mistype it, you'll get an error这里必须调用一个这样的参数sep ,如果你输入错误,你会得到一个错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM