Skip to content

Sending Connection-objects over multiprocessing connections fails #49142

Closed

Description

@gsson
mannequin
BPO 4892
Nosy @pitrou, @kristjanvalur, @jodal
Files
  • mp_pickle_conn.patch
  • mp_pickle_conn.patch
  • mp_pickle_conn.patch
  • mp_pickle_conn.patch
  • mp_pickle_conn.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2012-04-24.20:59:07.466>
    created_at = <Date 2009-01-09.11:46:06.880>
    labels = ['type-feature', 'library']
    title = 'Sending Connection-objects over multiprocessing connections fails'
    updated_at = <Date 2012-04-24.20:59:07.465>
    user = 'https://bugs.python.org/gsson'

    bugs.python.org fields:

    activity = <Date 2012-04-24.20:59:07.465>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-04-24.20:59:07.466>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2009-01-09.11:46:06.880>
    creator = 'gsson'
    dependencies = []
    files = ['25160', '25167', '25181', '25270', '25272']
    hgrepos = []
    issue_num = 4892
    keywords = ['patch']
    message_count = 31.0
    messages = ['79464', '79465', '79474', '84707', '91964', '91965', '91968', '91969', '126447', '157704', '157724', '157736', '157737', '157739', '157740', '157743', '157745', '157749', '157753', '157838', '157840', '157852', '157950', '157960', '157963', '158065', '158682', '158711', '158714', '159207', '159208']
    nosy_count = 11.0
    nosy_names = ['pitrou', 'kristjan.jonsson', 'jnoller', 'gsson', 'dsvensson', 'asksol', 'jodal', 'Jimbofbx', 'dragonfyre13', 'python-dev', 'sbt']
    pr_nums = []
    priority = 'low'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue4892'
    versions = ['Python 3.3']

    Activity

    1. gsson commented on Jan 9, 2009

      gssonmannequin
      MannequinAuthor

      It seems the old pyprocessing (http://pyprocessing.berlios.de/) can do
      some things that the new multiprocessing package can not;
      sending/receiving connection objects for one.

      This is a quite handy functionality, so it would be nice if it were
      reintroduced.

      Also, the error message isn't very helpful.

      Failing test below.

      $ python2.6 pipetest2.py 
      asdf
      Traceback (most recent call last):
        File "a.py", line 10, in <module>
          print c1.recv()
      TypeError: Required argument 'handle' (pos 1) not found
      $ PYTHONPATH=processing-0.52-py2.5-macosx-10.5-i386.egg python2.5 
      pipetest2.py 
      asdf
      Connection(handle=5)
      $ PYTHONPATH=multiprocessing-2.6.0.2-py2.5-macosx-10.5-i386.egg 
      python2.5 pipetest2.py 
      asdf
      Traceback (most recent call last):
        File "pipetest2.py", line 10, in <module>
          print c1.recv()
      TypeError: function takes at least 1 argument (0 given)
      $ uname -a
      Darwin midori.local 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 
      17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386
    2. added
      type-bug
      stdlib
      on Jan 9, 2009
    3. gsson commented on Jan 9, 2009

      gssonmannequin
      MannequinAuthor
      $ cat pipetest2.py 
      try:
      	from multiprocessing import Pipe
      except ImportError:
      	from processing import Pipe

      c1, c2 = Pipe(duplex=False)
      c2.send('asdf')
      print c1.recv()
      c2.send(c1)
      print c1.recv()

    4. self-assigned this
      on Jan 9, 2009
    5. jnoller commented on Jan 9, 2009

      jnollermannequin
      Mannequin

      thanks for filing this. I'll need to compare the two code bases and figure
      out why it's either regressed, or Richard removed it prior to the
      integration.

    6. added
      type-feature
      and removed
      type-bug
      on Jan 18, 2009
    7. jnoller commented on Mar 30, 2009

      jnollermannequin
      Mannequin

      Before I can logically support this, I need a clear use case that supports
      the idea that this should be supported in the current version of
      multiprocessing.

    8. dsvensson commented on Aug 26, 2009

      dsvenssonmannequin
      Mannequin

      A typical use case would be for a server to receive a connection, and
      then send that connection over to another process that does the actual
      work. This used to work with pyprocessing, and the support seems to be
      available in multiprocessing.c -> multiprocessing_sendfd, but not used.

    9. dsvensson commented on Aug 26, 2009

      dsvenssonmannequin
      Mannequin

      And to be clear, I have enabled connection pickling by issuing:

      multiprocessing.allow_connection_pickling()
    10. dsvensson commented on Aug 26, 2009

      dsvenssonmannequin
      Mannequin
    11. dsvensson commented on Aug 26, 2009

      dsvenssonmannequin
      Mannequin

      Ehm.. completly broken url in prev message.. Revision 65016, "Apply
      Amaury's patch to multiprocessing for bpo-3125, removes the copy_reg
      and replaces it with ForkingPickler.register(), which should resolve the
      conflict with the global registry/ctypes" is what I'm refering to.
      Without this patch, the reducers/rebuilders are properly registered in
      the pickler that connection.h (srsly, code in header files?) later uses.

    12. 11 remaining items

    13. Jimbofbx commented on Apr 7, 2012

      Jimbofbxmannequin
      Mannequin

      Shouldn't reduce_pipe_connection just be an alias for reduce_connection in unix so that using reduce_pipe_connection would work for both win and unix? My understanding after looking at the code is that reduce_pipe_connection isn't defined for non-win32, although I haven't tested it to see if that's true.

      Of course, ideally a pipe connection would just pickle and unpickle properly out-of-the-box, which I think was the original intent.

      Here's a complete, working example with Python 3.2 tested on Win 7 64-bit:

      import sys
      from multiprocessing import Process,Pipe, reduction
      
      def main():
          print("starting");
          i, o = Pipe(False)
          parent, child = Pipe();
          reducedchild = reduce_pipe(child);
          p = Process(target=helper, args=(i,));
          p.start();
          parent.send("hi");
          o.send(reducedchild);
          print(parent.recv());
          print("finishing");
          p.join();
          print("done");
      
      def helper(inPipe):
          childPipe = expand_reduced_pipe(inPipe.recv());
          childPipe.send("child got: " + childPipe.recv());
          return;
      
      def reduce_pipe(pipe):
          if sys.platform == "win32":
              return reduction.reduce_pipe_connection(pipe);
          else:
              return reduction.reduce_connection(pipe);
      
      def expand_reduced_pipe(reduced_pipe):
          return reduced_pipe[0](*reduced_pipe[1]);
      
      if __name__ == "__main__":
          main();
    14. sbt commented on Apr 9, 2012

      sbtmannequin
      Mannequin

      There is an undocumented function multiprocessing.allow_connection_pickling() whose docstring claims it allows connection and socket objects to be pickled.

      The attached patch fixes the multiprocessing.reduction module so that it works correctly. This means that TestPicklingConnections can be reenabled in the unit tests.

      The patch uses the new socket.share() and socket.fromshare() methods on Windows.

    15. pitrou commented on Apr 9, 2012

      @pitrou
      Member

      Unless there's a technical barrier, I still think it would be better to use ForkingPickler in multiprocessing.connection, rather than modify global state (copyreg). The pickling support is multiprocessing-specific and wouldn't make sense for other pickles (e.g. stored to disk).

    16. kristjanvalur commented on Apr 9, 2012

      kristjanvalurmannequin
      Mannequin

      I just want to point out that each time socket.share() is called, the resulting data can only be used once by socket.fromshare(). I'm mentioning this because I know there is some caching mechanism in reduction.py and that this data is not cacheable/reuseable.

    17. sbt commented on Apr 10, 2012

      sbtmannequin
      Mannequin

      Updated patch which uses ForkingPickler in Connection.send().

      Note that connection sharing still has to be enabled using allow_connection_pickling().

      Support could be enabled automatically, but that would introduce more circular imports which confuse me. It might be worthwhile refactoring to eliminate all circular imports.

    18. pitrou commented on Apr 10, 2012

      @pitrou
      Member

      Support could be enabled automatically, but that would introduce more
      circular imports which confuse me.

      Are you sure? AFAICT:

      • connection depends on forking
      • reduction depends on forking and connection

      But connection doesn't depend on reduction, neither does forking.

    19. sbt commented on Apr 10, 2012

      sbtmannequin
      Mannequin

      But connection doesn't depend on reduction, neither does forking.

      If registration of (Pipe)Connection is done in reduction then you can't make (Pipe)Connection picklable *automatically* unless you make connection depend on reduction (possibly indirectly).

      A circular import can be avoided by making reduction not import connection at module level. So not hard to fix.

    20. sbt commented on Apr 11, 2012

      sbtmannequin
      Mannequin

      The last patch did not work on Unix.

      Here is a new version where the reduction functions are automatically registered, so allow_connection_pickling() is redundant.

    21. pitrou commented on Apr 18, 2012

      @pitrou
      Member

      Could you regenerate your patch now that the win32 -> _winapi changes have been applied?

    22. sbt commented on Apr 19, 2012

      sbtmannequin
      Mannequin

      Up to date patch.

    23. sbt commented on Apr 19, 2012

      sbtmannequin
      Mannequin

      A couple of minor changes based on Antoine's earlier review (which I did not notice till now).

    24. python-dev commented on Apr 24, 2012

      python-devmannequin
      Mannequin

      New changeset 08d4c2fe51ea by Antoine Pitrou in branch 'default':
      Issue bpo-4892: multiprocessing Connections can now be transferred over multiprocessing Connections.
      http://hg.python.org/cpython/rev/08d4c2fe51ea

    25. pitrou commented on Apr 24, 2012

      @pitrou
      Member

      Thanks, Richard. I have now committed the patch. Hopefully the Windows buildbots will be ok :)

    26. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibtype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions