def shutdown(self): """ Convenience method to shutdown Receiver instance - it's background listener process. Create sender and send Shutdown message and terminate the process. """ logging.info("Shutting down %s ..." % self.__class__.__name__) # send itself Shutdown message to shutdown context = zmq.Context() # set up control channel contChann = context.socket(zmq.PUB) contChann.connect(self._controlAddr) contChann.send_json(ShutdownMsg()) contChann.close() # wait until the Receiver background process shuts count = 0 logging.info("Waiting for %s to finish ..." % self.__class__.__name__) while self._isReady: time.sleep(0.1) count += 1 if count > self.TIMEOUT_THREAD_FINISH * 10: # iterating by 10ths of a second logging.warn( "Receiver background process seems not shut yet, continue anyway ..." ) break logging.info("Shutdown %s finished." % self.__class__.__name__)
def sendShutdown(self): """ Tells the Receiver to shut down. This method mostly here for convenience in tests. """ self._contChannel.send_json(ShutdownMsg()) logging.debug("Shutdown message sent.")
def testReceiverShutdownByMessage(self): # start a Receiver rec = Receiver(self.addr, self.printer, self.ctrl) rec.startReceiver() # non blocking call workChann, contChann = self._getSenderChannels() # send some messages to the receiver and shut it eventually contChann.send_json(RegisterMsg("Receiver_t")) workChann.send_json(Alert(Type="Alert", Level=10)) contChann.send_json(UnregisterMsg("Receiver_t")) # terminate the Receiver contChann.send_json(ShutdownMsg()) # wait until the Receiver is properly shut # this will not be necessary when shutting down by a call while rec.isReady(): time.sleep(0.1)
def run(self): """ Start a sender and send some alert messages to the Receiver. """ context = zmq.Context() # set up a channel to send work sender = context.socket(zmq.PUSH) sender.connect(self.addr) controller = context.socket(zmq.PUB) controller.connect(self.ctrl) controller.send_json(RegisterMsg("Receiver_t")) for i in range(0, self.nAlerts): a = Alert(Type="Alert", Level=i) sender.send_json(a) controller.send_json(UnregisterMsg("Receiver_t")) controller.send_json(ShutdownMsg())
def testShutdownMsg(self): msg = ShutdownMsg() self.assertEqual(msg.key, "Shutdown") self.assertEqual(msg[msg.key], True)