如何修复或重组这种多处理模式以避免酸洗错误?
How to fix or reorganize this multiprocessing pattern to avoid pickling errors?
又一个pickling的问题...下面导致pickling错误。我认为这与范围界定或其他事情有关。我还不确定。
目标是拥有一个装饰器,它接受参数并用方法丰富函数。如果最好的方法是简单地显式构造 类 那么那很好,但这意味着对编写 "content".
的用户隐藏内容
import concurrent.futures
import functools
class A():
def __init__(self, fun, **kwargs):
self.fun = fun
self.stuff = kwargs
functools.update_wrapper(self, fun)
def __call__(self, *args, **kwargs):
print(self.stuff, args, kwargs)
return self.fun(*args, **kwargs)
def decorator(**kwargs):
def inner(fun):
return A(fun, **kwargs)
return inner
@decorator(a=1, b=2)
def f():
print('f called')
executor = concurrent.futures.ProcessPoolExecutor(max_workers=10)
tasks = [f for x in range(10)]
fut = list()
for task in tasks:
fut.append(executor.submit(task))
res = [x.result() for x in fut]
print(res)
错误是:
_pickle.PicklingError: Can't pickle <function f at 0x7fe37da121e0>: it's not the same object as __main__.f
我最终做了这样的事情:
def dill_wrapped(dilled, *args, **kwargs):
fun = dill.loads(dilled)
return wrapped(fun, *args, **kwargs)
您可能应该尽量避免使用它,但有时在装饰函数时确实需要它。
又一个pickling的问题...下面导致pickling错误。我认为这与范围界定或其他事情有关。我还不确定。
目标是拥有一个装饰器,它接受参数并用方法丰富函数。如果最好的方法是简单地显式构造 类 那么那很好,但这意味着对编写 "content".
的用户隐藏内容import concurrent.futures
import functools
class A():
def __init__(self, fun, **kwargs):
self.fun = fun
self.stuff = kwargs
functools.update_wrapper(self, fun)
def __call__(self, *args, **kwargs):
print(self.stuff, args, kwargs)
return self.fun(*args, **kwargs)
def decorator(**kwargs):
def inner(fun):
return A(fun, **kwargs)
return inner
@decorator(a=1, b=2)
def f():
print('f called')
executor = concurrent.futures.ProcessPoolExecutor(max_workers=10)
tasks = [f for x in range(10)]
fut = list()
for task in tasks:
fut.append(executor.submit(task))
res = [x.result() for x in fut]
print(res)
错误是:
_pickle.PicklingError: Can't pickle <function f at 0x7fe37da121e0>: it's not the same object as __main__.f
我最终做了这样的事情:
def dill_wrapped(dilled, *args, **kwargs):
fun = dill.loads(dilled)
return wrapped(fun, *args, **kwargs)
您可能应该尽量避免使用它,但有时在装饰函数时确实需要它。