Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; '"""': 0.07; 'posting.': 0.09; 'subject:method': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; '2.7': 0.14; 'posted': 0.15; 'a(object):': 0.16; 'arg):': 0.16; 'googled': 0.16; 'name)': 0.16; 'name):': 0.16; 'simplest': 0.16; 'subject:class': 0.16; 'windows?': 0.16; 'sender:addr:gmail.com': 0.17; 'thanks,': 0.17; 'wrote:': 0.18; 'seems': 0.21; 'import': 0.22; 'cc:addr:python.org': 0.22; '(by': 0.24; 'instance,': 0.24; 'proxy': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'skip:p 30': 0.29; "doesn't": 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; '3.2': 0.31; 'though.': 0.31; 'class': 0.32; 'linux': 0.33; 'skip:_ 10': 0.34; 'there,': 0.34; 'problem': 0.35; 'subject:with': 0.35; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'explains': 0.36; 'method': 0.36; 'thanks': 0.36; 'detail': 0.37; 'skip:_ 30': 0.39; 'skip:p 20': 0.39; 'august': 0.61; 'course': 0.61; 'further': 0.61; 'back': 0.62; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'details': 0.65; 'here': 0.66; 'skip:w 40': 0.68; '09:33,': 0.84; 'partial': 0.84; 'self.value': 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=Do4tUfwKnmIsCL+mIx7N6DFauLk618yvLxaFKWpFHYs=; b=twWyH8HRgpOqwtIBd/oWF7JOoQ6TSKHhoxBUm1C7Vi/geqF+ckJfrsOlgCHvFXyh5c xbWCKhO8tpq6MmNY5Kgg+ziXeAGSZKvPaw416xH1HyFJ5/6foeIfsmUalGRkwEn/oTQy quBxOzCdiOiRj50d7UYySIWXQsGbeavBUfPJZcQ6Z4fUiwH4qNklwF8D+zA79KijnBGC 2iJI5SUN5EZmyZR4GBUAyTDJiVpagc8ufTratL+pajFtiX5giiIySCtTzLnha2G+6F2S SA1JNp7O6SWRVTvdlsM4EUh0fl4xPpHnsJiJpjbrcot7MxbAMvWlWyYOj3vPKbxKp2zB sCSQ== X-Received: by 10.152.4.232 with SMTP id n8mr1087045lan.29.1375868866699; Wed, 07 Aug 2013 02:47:46 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> References: <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> From: Joshua Landau Date: Wed, 7 Aug 2013 10:47:06 +0100 X-Google-Sender-Auth: 0Ky8S3ViLFTBnh1QMlh4thhIiMA 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375868873 news.xs4all.nl 15876 [2001:888:2000:d::a6]:50495 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52127 On 7 August 2013 09:33, Luca Cerone wrote: > To correct my example: > > from multiprocessing import Pool > > class A(object): > def __init__(self,x): > self.value = x > def fun(self,x): > return self.value**x > > l = range(100) > p = Pool(4) > op = p.map(A(3).fun, l) > > doesn't work neither in Python 2.7, nor 3.2 (by the way I can't use Python 3 for my application). Are you using Windows? Over here on 3.3 on Linux it does. Not on 2.7 though. >> You will find that >> http://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-> > when-using-pythons-multiprocessing-pool-ma >> explains the problem in more detail than I understand. I suggest >> reading it and relaying further questions back to us. Or use Python 3 > > :) Thanks, but of course I googled and found this link before posting. I don't understand much of the details as well, that's why I posted here. > > Anyway, thanks for the attempt :) Reading there, the simplest method seems to be, in effect: 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 def _getattr_proxy_partialable(instance, name, arg): return getattr(instance, name)(arg) 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) l = range(100) p = Pool(4) op = p.map(getattr_proxy(A(3), "fun"), l) print(op)