Python namedtuple 作为 apply_async(..) 回调的参数

标签 python python-2.7 python-3.x python-multithreading

我正在编写一个简短的程序,我想在其中异步调用一个函数,这样它就不会阻塞调用者。为此,我使用了 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/

相关文章:

python - 无法在 xlsxwriter 中关闭工作表

arrays - np.around 和 np.round 实际上并不是四舍五入

Python 3 — 整数在 str() 转换后仍然是整数

python - 如果脚本使用 Python 2 运行,如何抛出异常?

python - 将一个系列分配给数据帧的所有列(按列)?

python - 如何压缩两个字符串?

python - getattr 和 unicode 属性

python - CancelledError : [_Derived_]RecvAsync is cancelled. 在 google colab 上运行

python - 如何将 json 字典列表解析为 csv

Python测试原始输入,if语句中的原始输入