def setup_module(): """ test alarm module setup """ global MRC_SEND MRC_SEND = rmr.rmr_init(b"4566", rmr.RMR_MAX_RCV_BYTES, 0x00) while rmr.rmr_ready(MRC_SEND) == 0: time.sleep(1) global MRC_RCV MRC_RCV = rmr.rmr_init(b"4567", rmr.RMR_MAX_RCV_BYTES, 0x00) while rmr.rmr_ready(MRC_RCV) == 0: time.sleep(1)
def setup_module(): """ test metric module setup """ global MRC_SEND MRC_SEND = rmr.rmr_init(b"4568", rmr.RMR_MAX_RCV_BYTES, 0x00) while rmr.rmr_ready(MRC_SEND) == 0: time.sleep(1) global MRC_RCV MRC_RCV = rmr.rmr_init(b"4569", rmr.RMR_MAX_RCV_BYTES, 0x00) while rmr.rmr_ready(MRC_RCV) == 0: time.sleep(1) # let all the RMR output appear time.sleep(2) mdc_logger.debug("RMR instances initialized")
def setup_module(): """ test_rmr module setup """ global MRC_SEND MRC_SEND = rmr.rmr_init(b"3562", rmr.RMR_MAX_RCV_BYTES, 0x00) while rmr.rmr_ready(MRC_SEND) == 0: time.sleep(1) global MRC_RCV MRC_RCV = rmr.rmr_init(b"3563", rmr.RMR_MAX_RCV_BYTES, 0x00) while rmr.rmr_ready(MRC_RCV) == 0: time.sleep(1) global MRC_BUF_RCV MRC_BUF_RCV = rmr.rmr_init(b"3564", rmr.RMR_MAX_RCV_BYTES, rmr.RMRFL_MTCALL) while rmr.rmr_ready(MRC_BUF_RCV) == 0: time.sleep(1)
def __init__(self, init_func_override=None, rcv_func_override=None): """ Init Parameters ---------- init_func_override: function (optional) Function that initializes RMR and answers an RMR context. Supply an empty function to skip initializing RMR. rcv_func_override: function (optional) Function that receives messages from RMR and answers a list. Supply a trivial function to skip reading from RMR. """ self.keep_going = True self.rcv_func = None self.last_ran = time.time() # see docs/overview#resiliency for a discussion of this self.instance_send_queue = queue.Queue() # thread safe queue https://docs.python.org/3/library/queue.html # queue for data delivery item self.ei_job_result_queue = queue.Queue() # intialize rmr context if init_func_override: self.mrc = init_func_override() else: mdc_logger.debug("Waiting for rmr to initialize..") # rmr.RMRFL_MTCALL puts RMR into a multithreaded mode, where a receiving thread # populates an internal ring of messages, and receive calls read from that. # currently the size is 2048 messages, so this is fine for the foreseeable future self.mrc = rmr.rmr_init(b"4562", rmr.RMR_MAX_RCV_BYTES, rmr.RMRFL_MTCALL) while rmr.rmr_ready(self.mrc) == 0: time.sleep(0.5) # set the receive function self.rcv_func = ( rcv_func_override if rcv_func_override else lambda: helpers.rmr_rcvall_msgs_raw(self.mrc, [A1_POLICY_RESPONSE, A1_POLICY_QUERY, A1_EI_QUERY_ALL, A1_EI_CREATE_JOB]) ) # start the work loop self.thread = Thread(target=self.loop) self.thread.start()
import os import signal import sys from ricxappframe.rmr import rmr # Demonstrate RMR cleanup def signal_handler(sig, frame): print("SIGINT received! Cleaning up RMR") rmr.rmr_close(mrc) print("Byeee") sys.exit(0) # Init rmr mrc = rmr.rmr_init(b"4562", rmr.RMR_MAX_RCV_BYTES, 0x00) while rmr.rmr_ready(mrc) == 0: time.sleep(1) print("not yet ready") rmr.rmr_set_stimeout(mrc, 2) sbuf = rmr.rmr_alloc_msg(mrc, 256) # capture ctrl-c signal.signal(signal.SIGINT, signal_handler) while True: # generate a random value between 1 and 256 bytes, then gen some random bytes with several nulls thrown in for val in [ "".join([ random.choice(string.ascii_letters + string.digits) for n in range(random.randint(1, 256))
from ricxappframe.rmr import rmr import time import sys import signal # Demonstrate RMR cleanup def signal_handler(sig, frame): print("SIGINT received! Cleaning up RMR") rmr.rmr_close(mrc) print("Byeee") sys.exit(0) # init rmr mrc = rmr.rmr_init("4560".encode("utf-8"), rmr.RMR_MAX_RCV_BYTES, 0x00) while rmr.rmr_ready(mrc) == 0: time.sleep(1) print("not yet ready") rmr.rmr_set_stimeout(mrc, 2) # capture ctrl-c signal.signal(signal.SIGINT, signal_handler) sbuf = None while True: print("Waiting for a message, will timeout after 2000ms") sbuf = rmr.rmr_torcv_msg(mrc, sbuf, 2000) summary = rmr.message_summary(sbuf) if summary[rmr.RMR_MS_MSG_STATE] == 12: print("Nothing received =(")
def __init__(self, port, wait_for_ready=True): """ sets up RMR, then launches a thread that reads and injects messages into a queue. Parameters ---------- port: int port to listen on wait_for_ready: bool (optional) If True, then this function hangs until RMR is ready to send, which includes having a valid routing file. This can be set to False if the client only wants to *receive only*. """ # Public # thread safe queue https://docs.python.org/3/library/queue.html # We use a thread and a queue so that a long running consume callback function can # never block reads. IE a consume implementation could take a long time and the ring # size for rmr blows up here and messages are lost. self.rcv_queue = queue.Queue() # RMR context; RMRFL_MTCALL puts RMR into a multithreaded mode, where a thread # populates a ring of messages that receive calls read from self.mrc = rmr.rmr_init( str(port).encode(), rmr.RMR_MAX_RCV_BYTES, rmr.RMRFL_MTCALL) if wait_for_ready: mdc_logger.debug( "Waiting for rmr to init on port {}..".format(port)) while rmr.rmr_ready(self.mrc) == 0: time.sleep(0.1) # Private self._keep_going = True # used to tell this thread to stop self._last_ran = time.time() # used for healthcheck self._loop_is_running = False # used in stop to know when it's safe to kill the mrc def loop(): mdc_logger.debug("Work loop starts") self._loop_is_running = True while self._keep_going: # read our mailbox # TODO: take a flag as to whether RAW is needed or not # RAW allows for RTS however the caller must free, and # the caller may not need RTS. Currently after # consuming, callers must call rmr.rmr_free_msg(sbuf) # Use a non-trivial timeout to avoid spinning the CPU. # The function returns if no messages arrive for that # interval, which allows a stop request to be processed. for (msg, sbuf) in helpers.rmr_rcvall_msgs_raw(self.mrc, timeout=5000): self.rcv_queue.put((msg, sbuf)) self._last_ran = time.time() self._loop_is_running = False mdc_logger.debug("Work loop ends") # start the work loop mdc_logger.debug("Starting loop thread") self._thread = Thread(target=loop) self._thread.start()