Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:41:19+00:00 2026-05-29T23:41:19+00:00

I would like to use a decorator on a function that I will subsequently

  • 0

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 : attribute lookup __builtin__.function failed”. I don’t quite see why it fails here. I feel certain that it’s something simple, but I can’t find it. Below is a minimal “working” example. I thought that using the functools function would be enough to let this work.

If I comment out the function decoration, it works without an issue. What is it about multiprocessing that I’m misunderstanding here? Is there any way to make this work?

Edit: After adding both a callable class decorator and a function decorator, it turns out that the function decorator works as expected. The callable class decorator continues to fail. What is it about the callable class version that keeps it from being pickled?

import random
import multiprocessing
import functools

class my_decorator_class(object):
    def __init__(self, target):
        self.target = target
        try:
            functools.update_wrapper(self, target)
        except:
            pass

    def __call__(self, elements):
        f = []
        for element in elements:
            f.append(self.target([element])[0])
        return f

def my_decorator_function(target):
    @functools.wraps(target)
    def inner(elements):
        f = []
        for element in elements:
            f.append(target([element])[0])
        return f
    return inner

@my_decorator_function
def my_func(elements):
    f = []
    for element in elements:
        f.append(sum(element))
    return f

if __name__ == '__main__':
    elements = [[random.randint(0, 9) for _ in range(5)] for _ in range(10)]
    pool = multiprocessing.Pool(processes=4)
    results = [pool.apply_async(my_func, ([e],)) for e in elements]
    pool.close()
    f = [r.get()[0] for r in results]
    print(f)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:41:21+00:00Added an answer on May 29, 2026 at 11:41 pm

    The problem is that pickle needs to have some way to reassemble everything that you pickle. See here for a list of what can be pickled:

    http://docs.python.org/library/pickle.html#what-can-be-pickled-and-unpickled

    When pickling my_func, the following components need to be pickled:

    • An instance of my_decorator_class, called my_func.

      This is fine. Pickle will store the name of the class and pickle its __dict__ contents. When unpickling, it uses the name to find the class, then creates an instance and fills in the __dict__ contents. However, the __dict__ contents present a problem…

    • The instance of the original my_func that’s stored in my_func.target.

      This isn’t so good. It’s a function at the top-level, and normally these can be pickled. Pickle will store the name of the function. The problem, however, is that the name "my_func" is no longer bound to the undecorated function, it’s bound to the decorated function. This means that pickle won’t be able to look up the undecorated function to recreate the object. Sadly, pickle doesn’t have any way to know that object it’s trying to pickle can always be found under the name __main__.my_func.

    You can change it like this and it will work:

    import random
    import multiprocessing
    import functools
    
    class my_decorator(object):
        def __init__(self, target):
            self.target = target
            try:
                functools.update_wrapper(self, target)
            except:
                pass
    
        def __call__(self, candidates, args):
            f = []
            for candidate in candidates:
                f.append(self.target([candidate], args)[0])
            return f
    
    def old_my_func(candidates, args):
        f = []
        for c in candidates:
            f.append(sum(c))
        return f
    
    my_func = my_decorator(old_my_func)
    
    if __name__ == '__main__':
        candidates = [[random.randint(0, 9) for _ in range(5)] for _ in range(10)]
        pool = multiprocessing.Pool(processes=4)
        results = [pool.apply_async(my_func, ([c], {})) for c in candidates]
        pool.close()
        f = [r.get()[0] for r in results]
        print(f)
    

    You have observed that the decorator function works when the class does not. I believe this is because functools.wraps modifies the decorated function so that it has the name and other properties of the function it wraps. As far as the pickle module can tell, it is indistinguishable from a normal top-level function, so it pickles it by storing its name. Upon unpickling, the name is bound to the decorated function so everything works out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a decorator that would add the decorated function to list, like this
I would like to use a language that I am familiar with - Java,
I would like to use a component that exposes the datasource property, but instead
I have a problem. I would like to use the animate() function to slide
I would like to implement a decorator that provides per-request caching to any method,
I am using AdoNetAppender (SQL server) in my asp.net application and would like use
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use as and is as members of an enumeration. I
I would like to use client-side Javascript to perform a DNS lookup (hostname to
I would like to use javascript to develop general-purpose GUI applications. Initially these are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.