def zmq_context(request): context = transport.Context() old_recv_msg = transport.Socket.recv_msg old_recv_addr_msg = transport.Socket.recv_addr_msg # Monkey-patch Socket so it times out on receive after 2 seconds, because # no receive in the test suite should ever take longer than that def recv_msg(self, flags=0): if not flags: if not self._socket.poll(2000, transport.POLLIN): raise IOError('timed out on receive') return old_recv_msg(self, flags) def recv_addr_msg(self, flags=0): if not flags: if not self._socket.poll(2000, transport.POLLIN): raise IOError('timed out on receive') return old_recv_addr_msg(self, flags) transport.Socket.recv_msg = recv_msg transport.Socket.recv_addr_msg = recv_addr_msg yield context transport.Socket.recv_msg = old_recv_msg transport.Socket.recv_addr_msg = old_recv_addr_msg context.close()
def __init__(self, config, hat): super().__init__() self._quit = False self.stick = hat.stick self.ctx = transport.Context() self.stick_queue = self.ctx.socket(transport.PUSH, protocol=protocols.sense_stick) self.stick_queue.hwm = 10 self.stick_queue.bind('inproc://stick')
def __call__(self, args=None): parser = terminal.configure_parser(__doc__, log_params=False) parser.add_argument( '--status-queue', metavar='ADDR', default=const.STATUS_QUEUE, help="The address of the queue used to report status to monitors " "(default: %(default)s)") parser.add_argument( '--control-queue', metavar='ADDR', default=const.CONTROL_QUEUE, dest='master_queue', help="The address of the queue a monitor can use to control the " "master (default: %(default)s)") parser.add_argument( '-r', '--rotate', metavar='DEGREES', default=0, type=int, help="The rotation of the HAT in degrees; must be 0 (the default) " "90, 180, or 270") try: config = parser.parse_args(args) config.control_queue = 'inproc://quit' except: # pylint: disable=bare-except return terminal.error_handler(*sys.exc_info()) with SenseHAT() as hat: hat.rotation = config.rotate ctx = transport.Context() quit_queue = ctx.socket(transport.PULL, protocol=protocols.task_control) quit_queue.bind(config.control_queue) try: stick = StickTask(config, hat) stick.start() screen = ScreenTask(config, hat) screen.start() msg, data = quit_queue.recv_msg() assert msg == 'QUIT' #signal.sigwait({signal.SIGINT, signal.SIGTERM}) except KeyboardInterrupt: pass finally: screen.quit() screen.join() stick.quit() stick.join() ctx.close() hat.screen.fade_to(array(Color('black')))
def __call__(self, args=None): parser = terminal.configure_parser(__doc__, log_params=False) parser.add_argument( '--status-queue', metavar='ADDR', default=const.STATUS_QUEUE, help="The address of the queue used to report status to monitors " "(default: %(default)s)") parser.add_argument( '--control-queue', metavar='ADDR', default=const.CONTROL_QUEUE, help="The address of the queue a monitor can use to control the " "master (default: %(default)s)") try: config = parser.parse_args(args) except: # pylint: disable=bare-except return terminal.error_handler(*sys.exc_info()) ctx = transport.Context() self.status_queue = ctx.socket(transport.SUB, protocol=reversed( protocols.monitor_stats)) self.status_queue.connect(config.status_queue) self.status_queue.subscribe('') sleep(1) self.ctrl_queue = ctx.socket(transport.PUSH, protocol=reversed( protocols.master_control)) self.ctrl_queue.connect(config.control_queue) self.ctrl_queue.send_msg('HELLO') try: self.loop = widgets.MainLoop(self.build_ui(), self.palette, event_loop=widgets.ZMQEventLoop(), unhandled_input=self.unhandled_input) self.loop.event_loop.watch_queue(self.status_queue, self.status_message) widgets.connect_signal(self.slave_list, 'modified', self.list_modified) self.loop.event_loop.alarm(1, self.tick) self.loop.run() finally: ctx.close() sys.stdout.flush()