Python 2.6 send connection object over Queue / Pipe / etc

后端 未结 2 1821
野性不改
野性不改 2020-12-30 08:02

Given this bug (Python Issue 4892) that gives rise to the following error:

>>> import multiprocessing
>>> multiprocessing.allow_connection_         


        
2条回答
  •  旧巷少年郎
    2020-12-30 08:39

    Here's roughly what I did:

    # Producer
    from multiprocessing.reduction import reduce_connection
    from multiprocessing import Pipe
    
       # Producer and Consumer share the Queue we call queue
    def handle(queue):
       reader, writer = Pipe()
       pickled_writer = pickle.dumps(reduce_connection(writer))
       queue.put(pickled_writer)
    

    and

    # Consumer
    from multiprocessing.reduction import rebuild_connection
    
    def wait_for_request():
        pickled_write = queue.get(block=True) # block=True isn't necessary, of course
        upw = pickle.loads(pickled_writer) # unpickled writer
        writer = upw[0](upw[1][0],upw[1][1],upw[1][2])
    

    The last line is cryptic, coming from the following:

    >>> upw
    (,
    (('/var/folders/.../pymp-VhT3wX/listener-FKMB0W',
    17, False), True, True))
    

    Hope that helps someone else. It works fine for me.

提交回复
热议问题