Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52134

Re: Using Pool map with a method of a class and a list

Path csiph.com!usenet.pasdenom.info!news.franciliens.net!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'url:pipermail': 0.05; '"""': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'function:': 0.09; 'items)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'def': 0.12; 'a(object):': 0.16; 'arg):': 0.16; 'arg,': 0.16; 'b):': 0.16; 'name)': 0.16; 'name):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:class': 0.16; 'tempted': 0.16; 'wrote:': 0.18; 'later': 0.20; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'either.': 0.24; 'instance,': 0.24; 'proxy': 0.24; 'header:X -Complaints-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'subject:list': 0.30; 'code': 0.31; 'class': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'subject:with': 0.35; "can't": 0.35; 'objects': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'module.': 0.36; "i'll": 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.38; 'skip:_ 30': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'url:mail': 0.40; 'august': 0.61; 'new': 0.61; 'course': 0.61; 'name': 0.63; 'skip:n 10': 0.64; 'our': 0.64; '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
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Using Pool map with a method of a class and a list
Date Wed, 07 Aug 2013 16:46:09 +0200
Organization None
References <96c575da-7601-4023-aa91-e80664f90333@googlegroups.com> <mailman.268.1375810736.1251.python-list@python.org> <4cff0d5e-33ab-42cd-b6d4-2b4fe235a274@googlegroups.com> <mailman.301.1375858161.1251.python-list@python.org> <021dfe24-af83-4307-856e-441cf35cb93a@googlegroups.com> <mailman.311.1375868873.1251.python-list@python.org> <13807c2e-7f9f-45dd-b36e-4cdc7cde6709@googlegroups.com> <CAN1F8qX5PCVsS-g03BTGYvm3opGNCg1giKC-O9cWADytqJogqw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084afdd.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.315.1375886779.1251.python-list@python.org> (permalink)
Lines 101
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375886779 news.xs4all.nl 15923 [2001:888:2000:d::a6]:47618
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52134

Show key headers only | View raw


Joshua Landau wrote:

> On 7 August 2013 11:10, Luca Cerone <luca.cerone@gmail.com> 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)
> 
> :)


There is also the copy_reg module. Adapting

<http://mail.python.org/pipermail/python-list/2008-July/469164.html>

you get:

import copy_reg
import multiprocessing
import new

def make_instancemethod(inst, methodname):
    return getattr(inst, methodname)

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)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Using Pool map with a method of a class and a list Luca Cerone <luca.cerone@gmail.com> - 2013-08-06 10:12 -0700
  Re: Using Pool map with a method of a class and a list Chris Angelico <rosuav@gmail.com> - 2013-08-06 18:38 +0100
    Re: Using Pool map with a method of a class and a list Luca Cerone <luca.cerone@gmail.com> - 2013-08-06 12:42 -0700
      Re: Using Pool map with a method of a class and a list Joshua Landau <joshua@landau.ws> - 2013-08-07 07:48 +0100
        Re: Using Pool map with a method of a class and a list Luca Cerone <luca.cerone@gmail.com> - 2013-08-07 01:33 -0700
          Re: Using Pool map with a method of a class and a list Joshua Landau <joshua@landau.ws> - 2013-08-07 10:47 +0100
            Re: Using Pool map with a method of a class and a list Luca Cerone <luca.cerone@gmail.com> - 2013-08-07 03:10 -0700
              Re: Using Pool map with a method of a class and a list Joshua Landau <joshua@landau.ws> - 2013-08-07 12:53 +0100
                Re: Using Pool map with a method of a class and a list Luca Cerone <luca.cerone@gmail.com> - 2013-08-07 15:26 -0700
                Re: Using Pool map with a method of a class and a list Joshua Landau <joshua@landau.ws> - 2013-08-07 23:49 +0100
              Re: Using Pool map with a method of a class and a list Peter Otten <__peter__@web.de> - 2013-08-07 16:46 +0200
              Re: Using Pool map with a method of a class and a list Joshua Landau <joshua@landau.ws> - 2013-08-07 16:52 +0100
              Re: Using Pool map with a method of a class and a list Peter Otten <__peter__@web.de> - 2013-08-07 18:15 +0200
                Re: Using Pool map with a method of a class and a list Luca Cerone <luca.cerone@gmail.com> - 2013-08-07 16:31 -0700
  Re: Using Pool map with a method of a class and a list Luca Cerone <luca.cerone@gmail.com> - 2013-08-06 12:37 -0700

csiph-web