Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.060 X-Spam-Evidence: '*H*': 0.89; '*S*': 0.01; '"""': 0.07; 'function:': 0.09; 'subject:method': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'a(object):': 0.16; 'arg):': 0.16; 'arg,': 0.16; 'name)': 0.16; 'name):': 0.16; 'subject:class': 0.16; 'tempted': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'later': 0.20; 'import': 0.22; 'cc:addr:python.org': 0.22; 'either.': 0.24; 'instance,': 0.24; 'proxy': 0.24; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'subject:with': 0.35; "can't": 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; "i'll": 0.36; 'skip:_ 30': 0.39; 'skip:p 20': 0.39; 'august': 0.61; 'course': 0.61; 'name': 0.63; 'our': 0.64; 'to:addr:gmail.com': 0.65; 'partial': 0.84; 'pickled': 0.84; 'self.value': 0.84; 'subject:Using': 0.84; 'do:': 0.91; 'same,': 0.91; 'works!': 0.91; 'write:': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=S7KOILWdar5IRZRKtvCX2Yb5G0dzZt7Qff4bMTKGPwY=; b=l6RVXxqshqzbwd4tPaW9GjYnY0xkL6QmdsJ3GRl8oXWejZ/hLKdifx9+R7EtlI+/Rn DG5uXB4r31bc2xHtVnlZpYrYlIgjBFp85yem8ubDnxt0GLkWxwi/2XE2Eu5eEQwggz/Y ciNDLTe7Lgga2hyOLIRuXnYct11YZf7SXKxQ8DvNwOOayQPCvj60g/mASTEH0PV0oM39 9S5KOId0m//wwIC6djuNZzWN10J7oVIxfX+hYCFSsWQ4+7YLPhuNOj2psAKAQ3D9OzdL 8wjkvjw3LFmATG5V2/fu9ms4Uqu++j4OzjBoc8NROfaSJHyjrjrQJUaffrgcVXKEnr0h uy6A== X-Received: by 10.152.121.73 with SMTP id li9mr1303073lab.42.1375876461032; Wed, 07 Aug 2013 04:54:21 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> From: Joshua Landau Date: Wed, 7 Aug 2013 12:53:40 +0100 X-Google-Sender-Auth: FZsbdJRdsXRpwbDZSr0XvjdwqHo Subject: Re: Using Pool map with a method of a class and a list To: Luca Cerone Content-Type: text/plain; charset=UTF-8 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375876463 news.xs4all.nl 15914 [2001:888:2000:d::a6]:48671 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52131 On 7 August 2013 11:10, Luca Cerone wrote: > I can't try it now, I'll let you know later if it works! > (Though just by reading I can't really understand what the code does). Well, >> from multiprocessing import Pool >> from functools import partial >> >> class A(object): >> def __init__(self,x): >> self.value = x >> def fun(self,x): >> return self.value**x This is all the same, as with >> l = range(100) >> p = Pool(4) You then wanted to do: > op = p.map(A(3).fun, l) but bound methods can't be pickled, it seems. However, A(3) *can* be pickled. So what we want is a function: def proxy(arg): A(3).fun(arg) so we can write: > op = p.map(proxy, l) To generalise you might be tempted to write: def generic_proxy(instance, name): def proxy(arg): # Equiv. of instance.name(arg) getattr(instance, name)(arg) but the inner function won't work as functions-in-functions can't be pickled either. So we use: >> def _getattr_proxy_partialable(instance, name, arg): >> return getattr(instance, name)(arg) Which takes all instance, name and arg. Of course we only want our function to take arg, so we partial it: >> def getattr_proxy(instance, name): >> """ >> A version of getattr that returns a proxy function that can >> be pickled. Only function calls will work on the proxy. >> """ >> return partial(_getattr_proxy_partialable, instance, name) partial objects are picklable, btw. >> op = p.map(getattr_proxy(A(3), "fun"), l) >> print(op) :)