Python decorator with multiprocessing fails

前端 未结 3 445
醉话见心
醉话见心 2020-12-17 10:38

I would like to use a decorator on a function that I will subsequently pass to a multiprocessing pool. However, the code fails with \"PicklingError: Can\'t pickle : attribut

3条回答
  •  时光说笑
    2020-12-17 11:01

    I also had some problem using decorators in multiprocessing. I'm not sure if it's the same problem as yours:

    My code looked like this:

    from multiprocessing import Pool
    
    def decorate_func(f):
        def _decorate_func(*args, **kwargs):
            print "I'm decorating"
            return f(*args, **kwargs)
        return _decorate_func
    
    @decorate_func
    def actual_func(x):
        return x ** 2
    
    my_swimming_pool = Pool()
    result = my_swimming_pool.apply_async(actual_func,(2,))
    print result.get()
    

    and when I run the code I get this:

    Traceback (most recent call last):
      File "test.py", line 15, in 
        print result.get()
      File "somedirectory_too_lengthy_to_put_here/lib/python2.7/multiprocessing/pool.py", line 572, in get
        raise self._value
    cPickle.PicklingError: Can't pickle : attribute lookup __builtin__.function failed
    

    I fixed it by defining a new function to wrap the function in the decorator function, instead of using the decorator syntax

    from multiprocessing import Pool
    
    def decorate_func(f):
        def _decorate_func(*args, **kwargs):
            print "I'm decorating"
            return f(*args, **kwargs)
        return _decorate_func
    
    def actual_func(x):
        return x ** 2
    
    def wrapped_func(*args, **kwargs):
        return decorate_func(actual_func)(*args, **kwargs)
    
    my_swimming_pool = Pool()
    result = my_swimming_pool.apply_async(wrapped_func,(2,))
    print result.get()
    

    The code ran perfectly and I got:

    I'm decorating
    4
    

    I'm not very experienced at Python, but this solution solved my problem for me

提交回复
热议问题