Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"__main__":': 0.09; '__name__': 0.09; 'deprecated': 0.09; 'items)': 0.09; 'method:': 0.09; 'subject:method': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'a(object):': 0.16; 'b):': 0.16; 'hack,': 0.16; 'lambda': 0.16; 'subject:class': 0.16; 'to:addr:web.de': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'replace': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'class': 0.32; 'this.': 0.32; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'received:google.com': 0.35; 'two': 0.37; 'delete': 0.39; 'skip:p 20': 0.39; 'august': 0.61; 'new': 0.61; 'skip:n 10': 0.64; 'otten': 0.84; 'subject:Using': 0.84; '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=uQfFiW4ZPHJO76bVsh/aSh/cQM8TI2syU+UKxV4gWSY=; b=O/yVUAbYkqTr5kETwPxdlXHedCMc9g2y66yNvjnOxG9mwPQcngpD5T+4WkAOeon3a+ bwQ5B+yyuDDfwBUgEejhPCaVJATXDiHUlBfxVZ1GGhlqJkUGjTDa0S8FUobg+/ZlngCC 0jXCJypfV6C4TrKOdsl0xMDLpZX4Ih2SRHZ/LnZXY6HSIKhRB1VDn263KlV0aFuLMbsl QlqvuuXAWbc/tBNNqMS2wi6sxlhyLn51Qj6RTTHgyCFYrn2tYKw0oJGX+XdhAlhHq66P yVwwTDPmRUF2/J/QI01IofcbVyRpdaD/xBEt9aQWnUsn0NfL4BwnTc5EmT7a+3lz3fKo 5a0Q== X-Received: by 10.112.33.205 with SMTP id t13mr529424lbi.22.1375890805158; Wed, 07 Aug 2013 08:53:25 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: 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 16:52:45 +0100 X-Google-Sender-Auth: RZBa86418HiIkrd2vQen0oTxNZE Subject: Re: Using Pool map with a method of a class and a list To: Peter Otten <__peter__@web.de> 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375891216 news.xs4all.nl 15890 [2001:888:2000:d::a6]:33618 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52135 On 7 August 2013 15:46, Peter Otten <__peter__@web.de> wrote: > import copy_reg > import multiprocessing > import new "new" is deprecated from 2.6+; use types.MethodType instead of new.instancemethod. > def make_instancemethod(inst, methodname): > return getattr(inst, methodname) This is just getattr -- you can replace the two uses of make_instancemethod with getattr and delete this ;). > def pickle_instancemethod(method): > return make_instancemethod, (method.im_self, method.im_func.__name__) > > copy_reg.pickle( > new.instancemethod, pickle_instancemethod, make_instancemethod) > > class A(object): > def __init__(self, a): > self.a = a > def fun(self, b): > return self.a**b > > if __name__ == "__main__": > items = range(10) > pool = multiprocessing.Pool(4) > print pool.map(A(3).fun, items) Well that was easy. The Stackoverflow link made that look *hard*. -1 to my hack, +1 to this. You can do this in one statement: copy_reg.pickle( types.MethodType, lambda method: (getattr, (method.im_self, method.im_func.__name__)), getattr )