python - 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
我看过一些文章解释这是一个酸洗问题,但我无法完全理解这里发生了什么。我不想切换到不使用装饰器,但我愿意使用多处理库以外的东西。
解决方案
推荐阅读
- python - 尝试在 python 中使用子进程时出现分段错误(核心转储)
- jquery - Selenium + Firefox + Geckodriver 不加载 jQuery
- c# - 创建一个敌人类
- c# - 如何使用 Nreco LambdaParser 包使用按位运算符解析字符串表达式?
- python - python浮点数的精度(小数点)是多少?
- firebase - Flutter Firestore 将字段保存为字符串?
- python - 日期时间:TypeError:'builtin_function_or_method'和'builtin_function_or_method'的实例之间不支持'<'
- python - 使用保存的训练模型在新数据集上进行预测
- node.js - 使用 $push 时如何验证数组长度?
- javascript - 在Javascript中计算数组中的数据类型