def _loop(mailbox, channels, archive_name): """Main server loop. Intended to be a Process target (and private to this module). Accepts messages through its mailbox queue, and takes the appropriate action based on the command and parameters contained within the message. Parameters ---------- mailbox : Queue Used for inter-process communication. channels : list of str list of channel names in the underlying data table. Any records written to the buffer are expected to have an entry for each channel. archive_name : str sqlite database name """ buf = Buffer(channels=channels, archive_name=archive_name) while True: # Messages should be tuples with the structure: # (sender, (command, params)) # where sender is either None or a Queue. msg = mailbox.get() sender, body = msg command, params = body if command == MSG_EXIT: delete_archive = params buf.cleanup(delete_archive=delete_archive) sender.put(('exit', 'ok')) break elif command == MSG_PUT: record = params buf.append(record) elif command == MSG_GET_ALL: sender.put(buf.all()) elif command == MSG_COUNT: sender.put(len(buf)) elif command == MSG_QUERY_SLICE: start, end, field = params logging.debug("Sending query: {}".format((start, end, field))) sender.put(buf.query(start, end, field)) elif command == MSG_QUERY: # Generic query filters, ordering, max_results = params sender.put(buf.query_data(filters, ordering, max_results)) elif command == MSG_STARTED: sender.put(('started', 'ok')) else: logging.debug("Error; message not understood: {}".format(msg))