首页 > 解决方案 > Python 多处理装饰器在酸洗时失败

问题描述

我的用例:我想要一个fire and forget函数装饰器,它只会异步调用另一个函数。

这是我现在拥有的:

from multiprocessing import Process

def fire_and_forget(func):
   def inner(*args, **kwargs):
      proc = Process(target=func, args=args, kwargs=kwargs)
      proc.start()
      proc.join()
   return inner

很简单——一个典型的 python 装饰器,它只是为函数创建一个新进程并启动它。我相信这会给我我正在寻找的异步功能。

现在,我用这个装饰一个测试函数:

@fire_and_forget
def my_test_function(name, age=24):
   print(f'Name: {name}, Age: {age}')

my_test_function('John')

但是,我收到以下错误:

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    my_test_function('John')
  File "<pyshell#3>", line 4, in inner
    proc.start()
  File "C:\Users\John\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\John\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\context.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\John\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\John\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\John\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function my_test_function at 0x03C93810>: it's not the same object as __main__.my_test_function

我看过一些文章解释这是一个酸洗问题,但我无法完全理解这里发生了什么。我不想切换到不使用装饰器,但我愿意使用多处理库以外的东西。

标签: pythonpython-3.xmultiprocessing

解决方案


推荐阅读