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


Groups > comp.lang.python > #52135

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

References (4 earlier) <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> <kttmj9$ll4$1@ger.gmane.org>
From Joshua Landau <joshua@landau.ws>
Date 2013-08-07 16:52 +0100
Subject Re: Using Pool map with a method of a class and a list
Newsgroups comp.lang.python
Message-ID <mailman.316.1375891216.1251.python-list@python.org> (permalink)

Show all headers | View raw


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
)

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