非阻塞multiprocessing.connection.Listener吗? [英] Non-blocking multiprocessing.connection.Listener?

查看:173
本文介绍了非阻塞multiprocessing.connection.Listener吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用multiprocessing.connection.Listener在进程之间进行通信,这对我来说是一种魅力.现在我真的很喜欢我的mainloop在客户端的命令之间做其他事情.不幸的是,listener.accept()会阻止执行,直到建立与客户端进程的连接为止.

I use multiprocessing.connection.Listener for communication between processes, and it works as a charm for me. Now i would really love my mainloop to do something else between commands from client. Unfortunately listener.accept() blocks execution until connection from client process is established.

是否有一种简单的方法来管理multiprocessing.connection的非阻塞检查?暂停?还是我应该使用专用线程?

Is there a simple way of managing non blocking check for multiprocessing.connection? Timeout? Or shall i use a dedicated thread?

    # Simplified code:

    from multiprocessing.connection import Listener

    def mainloop():
        listener = Listener(address=(localhost, 6000), authkey=b'secret')

        while True:
            conn = listener.accept() # <---  This blocks!
            msg = conn.recv() 
            print ('got message: %r' % msg)
            conn.close()

推荐答案

我自己没有使用Listener对象-为此任务,我通常使用multiprocessing.Queue; Doco位于以下链接:

I've not used the Listener object myself- for this task I normally use multiprocessing.Queue; doco at the following link:

https://docs.python.org/2/library/queue.html#Queue.Queue

该对象可用于通过一个不错的API在Python进程之间发送和接收任何可腌制的对象;我想您会对以下内容最感兴趣:

That object can be used to send and receive any pickle-able object between Python processes with a nice API; I think you'll be most interested in:

  • 在过程A中
    • .put('some message')
    • in process A
      • .put('some message')
      • .get_nowait() # will raise Queue.Empty if nothing is available- handle that to move on with your execution

      唯一的限制是您需要在某个时候控制两个Process对象,以便能够将队列分配给它们-像这样:

      The only limitation with this is you'll need to have control of both Process objects at some point in order to be able to allocate the queue to them- something like this:

      import time
      from Queue import Empty
      from multiprocessing import Queue, Process
      
      
      def receiver(q):
          while 1:
              try:
                  message = q.get_nowait()
                  print 'receiver got', message
              except Empty:
                  print 'nothing to receive, sleeping'
                  time.sleep(1)
      
      
      def sender(q):
          while 1:
              message = 'some message'
              q.put('some message')
              print 'sender sent', message
              time.sleep(1)
      
      
      some_queue = Queue()
      
      process_a = Process(
          target=receiver,
          args=(some_queue,)
      )
      
      process_b = Process(
          target=sender,
          args=(some_queue,)
      )
      
      process_a.start()
      process_b.start()
      
      print 'ctrl + c to exit'
      try:
          while 1:
              time.sleep(1)
      except KeyboardInterrupt:
          pass
      
      process_a.terminate()
      process_b.terminate()
      
      process_a.join()
      process_b.join()
      

      队列之所以不错,是因为您实际上可以根据自己的喜好,完全有相同的Queue对象拥有尽可能多的使用者和生产者(方便分配任务).

      Queues are nice because you can actually have as many consumers and as many producers for that exact same Queue object as you like (handy for distributing tasks).

      我应该指出,仅在Process上调用.terminate()是错误的形式-您应该使用闪亮的新消息传递系统传递关闭消息或类似性质的消息.

      I should point out that just calling .terminate() on a Process is bad form- you should use your shiny new messaging system to pass a shutdown message or something of that nature.

      这篇关于非阻塞multiprocessing.connection.Listener吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆