호출자를 차단하지 않도록 함수를 비동기 적으로 호출하려는 간단한 프로그램을 작성하고 있습니다. 이렇게하려면 파이썬의 multiprocessing
모듈에있는 Pool
을 사용하고 있습니다.apply_async (..) callback에 대한 인수로 Python namedtuple
비동기 적으로 호출되는 함수에서 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
반환 값을 전달하는 방법이 있는지
사람이 알고 있나요? 내가하고있는 일에 더 나은/불쾌한 접근법이 있습니까?
* * 정의 된 명명 된 튜플은 잘 절인 할 수 있습니다. [namedtuple 인스턴스를 올바르게 pickle하는 법] (// stackoverflow.com/q/16377215) –