[英]Using multiprocessing pool in Python
有人可以指出这段代码的错误。 它没有给出任何结果。
import multiprocessing
results = []
def log_results(result):
results.append(result)
def multiply(x, y):
print(f"Gets here for process name {multiprocessing.current_process().name()}")
return x * y
if __name__ == "__main__":
pool = multiprocessing.Pool()
numbers = [(1,1), (2,2), (3,3)]
for x, y in numbers:
print (f"Checking x {x} and y {y}")
pool.apply_async(multiply, (x, y), callback=log_results)
pool.close()
pool.join()
print(results)
结果是一个空列表,在这种情况下不应该是正确的? 我使用了apply_async和map_async。 两者都没有给出正确的输出。 有人可以帮我这里
编辑:您对代码进行了编辑,所以现在我的答案已经过时了。 我认为唯一需要做的两件事是:
error_callback
因为我仍然认为你需要确保默认情况下写入的池不会无声地失败。 multiprocessing.current_process().name()
重写为multiprocessing.current_process().name
。 所以:
import multiprocessing
results = []
def log_results(result):
results.append(result)
def log_e(e):
print(e)
def multiply(x, y):
print(f"Gets here for process name {multiprocessing.current_process().name}")
return x * y
pool = multiprocessing.Pool()
numbers = [(1,1), (2,2), (3,3)]
for x, y in numbers:
print (f"Checking x {x} and y {y}")
pool.apply_async(multiply, (x, y), callback=log_results,
error_callback=log_e)
pool.close()
pool.join()
print(results)
老答案
这让我疯狂了一会然后才有意义。
如果我用这样的multiply
改变运行它:
def multiply(nums):
print("print")
return nums[0] * nums[1]
它运行正常。 你在评论中说过“我认为函数multiply
不是首先调用的。” 这是因为指定了callback
error_callback
指定error_callback
。 省略错误回调的结果是您的脚本无声地失败。
您可以通过以下方式检查:
import multiprocessing
results = []
def log_results(result):
print(result)
def log_e(e):
print(e)
def multiply(x, y):
print(f"Gets here for process name {multiprocessing.current_process().name()}")
return x * y
pool = multiprocessing.Pool()
numbers = [[1,1], [2,2], [3,3]]
mapResult = pool.map_async(multiply, numbers, callback=log_results,
error_callback=log_e)
pool.close()
pool.join()
这使:
multiply() missing 1 required positional argument: 'y'
并且像这样multiply
:
def multiply(nums):
return nums[0] * nums[1]
然后返回[1, 4, 9]
PS我正在运行Python 3.6.7
因此,您的当前代码实际上是因为以下行而失败:
print(f"Gets here for process name {multiprocessing.current_process().name()}")
它出错为TypeError: 'str' object is not callable
,不是因为你调用multiply()
的方式有什么
如果你删除它:
import multiprocessing
results = []
def log_results(result):
results.append(result)
def multiply(x, y):
# print(f"Gets here for process name {multiprocessing.current_process().name()}")
return x * y
if __name__ == "__main__":
pool = multiprocessing.Pool()
numbers = [(1,1), (2,2), (3,3)]
for x, y in numbers:
print (f"Checking x {x} and y {y}")
pool.apply_async(multiply, (x, y), callback=log_results)
pool.close()
pool.join()
print(results)
它返回:
Checking x 1 and y 1
Checking x 2 and y 2
Checking x 3 and y 3
[1, 4, 9]
所以,如果你真的隔离你的print(f)
:
print(multiprocessing.current_process().name())
你得到错误: TypeError: 'str' object is not callable
因为
multiprocessing.current_process()
实际上是一个进程对象,其name
作为对象的属性,返回一个字符串(感谢darkonaut)字符串。 您试图将.name()
作为函数调用,但它是一个属性。
因此,如果您将函数更改为包含.name
,而不是.name()
:
import multiprocessing
results = []
def log_results(result):
results.append(result)
def multiply(x, y):
print(f"Gets here for process name {multiprocessing.current_process().name}")
return x * y
if __name__ == "__main__":
pool = multiprocessing.Pool()
numbers = [(1,1), (2,2), (3,3)]
for x, y in numbers:
print (f"Checking x {x} and y {y}")
pool.apply_async(multiply, (x, y), callback=log_results)
pool.close()
pool.join()
print(results)
你回来了:
Checking x 1 and y 1
Checking x 2 and y 2
Checking x 3 and y 3
Gets here for process name ForkPoolWorker-1
Gets here for process name ForkPoolWorker-2
Gets here for process name ForkPoolWorker-3
[1, 4, 9]
这是你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.