我正在编写一个简短的程序,我想在其中异步调用一个函数,这样它就不会阻塞调用者。为此,我使用了 python 的 multiprocessing
模块中的 Pool
。
在异步调用的函数中,我想返回一个 namedtuple
以符合我程序其余部分的逻辑,但我发现 namedtuple
确实似乎不是从派生进程传递到回调的受支持类型(可能是因为它不能被腌制)。这是问题的最小重现。
from multiprocessing import Pool
from collections import namedtuple
logEntry = namedtuple("LogEntry", ['logLev', 'msg'])
def doSomething(x):
# Do actual work here
logCode = 1
statusStr = "Message Here"
return logEntry(logLev=logCode, msg=statusStr)
def callbackFunc(result):
print(result.logLev)
print(result.msg)
def userAsyncCall():
pool = Pool()
pool.apply_async(doSomething, [1,2], callback=callbackFunc)
if __name__ == "__main__":
userAsyncCall() # Nothing is printed
# If this is uncommented, the logLev and status are printed as expected:
# y = logEntry(logLev=2, msg="Hello World")
# callbackFunc(y)
有谁知道是否有办法将 namedtuple
返回值从异步进程传递到回调?对于我正在做的事情,是否有更好/更 pythonic 的方法?
最佳答案
问题在于 namedtuple()
的返回值及其 typename
参数的大小写不同。也就是说,命名元组的类定义与您给它的变量名不匹配。您需要两者匹配:
LogEntry = namedtuple("LogEntry", ['logLev', 'msg'])
并相应地更新doSomething()
中的return
语句。
完整代码:
from multiprocessing import Pool
from collections import namedtuple
LogEntry = namedtuple("LogEntry", ['logLev', 'msg'])
def doSomething(x):
# Do actual work here
logCode = 1
statusStr = "Message Here"
return LogEntry(logLev=logCode, msg=statusStr)
def callbackFunc(result):
print(result.logLev)
print(result.msg)
def userAsyncCall():
pool = Pool()
return pool.apply_async(doSomething, [1], callback=callbackFunc)
if __name__ == "__main__":
c = userAsyncCall()
# To see whether there was an exception, you can attempt to get() the AsyncResult object.
# print c.get()
(要查看类定义,请将 verbose=True
添加到 namedtuple()
。)
关于Python namedtuple 作为 apply_async(..) 回调的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41831281/