<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <title></title>
  </head>
  <body bgcolor="#ffffff" text="#000000">
    On 16/12/2010 11:09, Dimitrios Pritsos wrote:
    <blockquote cite="mid:4D09F375.40208@extremepro.gr" type="cite">
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <br>
      Hello Core Developers, <br>
      <br>
      My name is Dimitrios and I am newbie in python. I am working on a
      Project (part of my PhD) that is called Synergeticprocessing
      module. Initially is imitating the multiprocessing built in module
      but the processes are distributed on a LAN and not Locally. The
      main issue I have is with Pickle module. And I think I found some
      kind of BUG in the built in multiprocessing module. <br>
      <br>
    </blockquote>
    <br>
    Hello Dimitrios,<br>
    <br>
    Please post your bug report to the Python bug tracker. As you think
    you have a fix for the issue it is much more likely to be applied
    quickly if you can package it in the form of a test that
    demonstrates the issue and a patch that fixes it.<br>
    <br>
    <a class="moz-txt-link-freetext" href="http://bugs.python.org/">http://bugs.python.org/</a><br>
    <br>
    All the best,<br>
    <br>
    Michael Foord<br>
    <br>
    <br>
    <br>
    <blockquote cite="mid:4D09F375.40208@extremepro.gr" type="cite">
      (Synergeticprocessing module is located at GitHub: <a
        moz-do-not-send="true" class="moz-txt-link-freetext"
        href="https://github.com/dpritsos/synergeticprocessing">https://github.com/dpritsos/synergeticprocessing</a>)<br>
      <br>
      Starting with the "BUG". In case someone uses the
      multiprocessing.Pool of processes he/she has to face the problem
      of types.MehtodType Impossible pickling. That is you cannot
      dispatch an class instance method to the to the Process Pool.
      However, digging in to the Source Code of the module there are few
      lines that resolve this issue however are not activated or they
      are faultily activated so they do not work. This is the 'BUG'<br>
      <br>
      <u>@ ....../multiprocessing/forking.py</u><br>
      <br>
      <br>
      .<br>
      .<br>
      .<br>
      <br>
      #<br>
      # Try making some callable types picklable<br>
      #<br>
      <br>
      <font color="#ff0000">from pickle import Pickler<br>
        class ForkingPickler(Pickler):<br>
        &nbsp;&nbsp;&nbsp; dispatch = Pickler.dispatch.copy()<br>
        <br>
        &nbsp;&nbsp;&nbsp; @classmethod<br>
        &nbsp;&nbsp;&nbsp; def register(cls, type, reduce):<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def dispatcher(self, obj):<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rv = reduce(obj)<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.save_reduce(obj=obj, *rv)<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cls.dispatch[type] = dispatcher</font><br>
      <br>
      <font color="#33cc00">def _reduce_method(m):<br>
        &nbsp;&nbsp;&nbsp; if m.im_self is None:<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getattr, (m.im_class, m.im_func.func_name)<br>
        &nbsp;&nbsp;&nbsp; else:<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getattr, (m.im_self, m.im_func.func_name)<br>
        ForkingPickler.register(type(ForkingPickler.save),
        _reduce_method)</font><br>
      <br>
      def _reduce_method_descriptor(m):<br>
      &nbsp;&nbsp;&nbsp; return getattr, (m.__objclass__, m.__name__)<br>
      <font color="#ff0000">ForkingPickler.register(type(list.append),
        _reduce_method_descriptor)</font><br>
      ForkingPickler.register(type(int.__add__),
      _reduce_method_descriptor)<br>
      <br>
      #def _reduce_builtin_function_or_method(m):<br>
      #&nbsp;&nbsp;&nbsp; return getattr, (m.__self__, m.__name__)<br>
      #ForkingPickler.register(type(list().append),
      _reduce_builtin_function_or_method)<br>
      #ForkingPickler.register(type(int().__add__),
      _reduce_builtin_function_or_method)<br>
      .<br>
      .<br>
      .<br>
      <br>
      The <font color="#ff0000">RED</font> lines are not doing the job,
      for some reason they are not managing to register the <font
        color="#33cc00">GREEN</font> function as a global
      reduce/pickling function even if you call the registration
      function into you __main__ script. <br>
      <br>
      The solution I found is just to do this <br>
      <b><br>
        import copy_reg<br>
        import types</b> <br>
      <br>
      <font color="#33cc00"><font color="#000000">def _reduce_method(m):<br>
          &nbsp;&nbsp;&nbsp; if m.im_self is None:<br>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getattr, (m.im_class, m.im_func.func_name)<br>
          &nbsp;&nbsp;&nbsp; else:<br>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getattr, (m.im_self, m.im_func.func_name)<br>
          <br>
          <b>copy_reg.pickle(types.MethodType, _reduce_method)</b><br>
          .<br>
          .<br>
          .</font><br>
        <br>
        <font color="#000000">Doing that everything works FINE. But ONLY
          for local methods i.e. the ones that their class is defined on
          the __main__ script or other import-ed.<br>
          <br>
          In case you want to send something remotely (in an other
          machine) or to an other __main__ script running separately
          then you get a message like this:<br>
          <br>
          <font color="#ff0000">'module' object has no attribute
            '&lt;my_class&gt;'</font><br>
          <br>
          The only way to resolve this is firstly to import a script
          that has &lt;my_class&gt; defined there and everything works
          fine. <br>
          <br>
          SO the problems it seems to be that the <b>m.im_class</b>&nbsp;
          (look code above) has some attribute __module__ defined as
          __module__ = '__main__' or something like that. And this is
          the reason why remote script cannot execute the function. I
          mean that the _reduce_method() above DOES is pickling the
          whole CLASS object so there is no reason not to be executed at
          the remote script. Besides it does as mentioned above in you
          just import this the user defined class form an other script.
          <br>
          <br>
          <br>
          I have already spent about 12 weeks working on building my
          synergeticPool and resolve the issue of Pickling and only 2
          days needed for the code of the Pool and the rest of the time
          was spent for the Pickling issues, and study all the Class
          related mechanics of python. That was the reason I ve started
          digging the multipocessessing module and found this say 'BUG',
          and finally sent this email. <br>
          <br>
          Best Regards, <br>
          <br>
          <br>
          Dimitrios <br>
          &nbsp;<br>
        </font></font><br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
Python-Dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Python-Dev@python.org">Python-Dev@python.org</a>
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/python-dev">http://mail.python.org/mailman/listinfo/python-dev</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk">http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk</a>
</pre>
    </blockquote>
    <br>
    <br>
    <pre class="moz-signature" cols="72">-- 

<a class="moz-txt-link-freetext" href="http://www.voidspace.org.uk/">http://www.voidspace.org.uk/</a>

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing <a class="moz-txt-link-freetext" href="http://www.sqlite.org/different.html">http://www.sqlite.org/different.html</a>
</pre>
  </body>
</html>