python - 当装饰器遇到multiprocessing, 出了点bug.
天蓬老师
天蓬老师 2017-04-18 10:30:27
0
3
729
from multiprocessing import Pool

def with_app_context(need_new=False):
    def magic(func):
        app_store = {}
        def wrapper(*args, **kwargs):
            print 'inside', args, kwargs
            result = func(*args, **kwargs)
            return result

        return wrapper

    return magic


@with_app_context(need_new=True)
def func(k1, k2):
    print k1, k2
    return 'ret'


pool = Pool(processes=1)
pool.apply_async(func, args=('hi', 'yo'))
pool.close()
pool.join()

什么结果都不输出, 请问哪有bug?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
Peter_Zhu

It seems that for this reason, function decorators cannot be pickled
You can take a look at this:
Decorators and multi-processes and Pickle

  • Extension: Python MultiProcessing usage experience

阿神

I have found the reason:

Traceback (most recent call last):
  File "wrapper.py", line 30, in <module>
    print ret.get()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 567, in get
    raise self._value
cPickle.PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

Exception in child process.

洪涛

http://stackoverflow.com/ques...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template