def get(self, block=True, timeout=None): """ Remove and return an item from the queue. If *block* is ``True`` and *timeout* is ``None`` (the default), block if necessary until an item is available. If *timeout* is a positive number, it blocks at most *timeout* seconds and raises the :class:`queue.Empty` exception if no item was available within that time. Otherwise (block is ``False``), return an item if one is immediately available, else raise the :class:`queue.Empty` exception (*timeout* is ignored in that case). """ if not block: timeout = 0.0 elif timeout is None: timeout = float('inf') buffer = ffi.new('char[]', self._max_msg_size) size = ffi.new('size_t *', self._max_msg_size) priority = ffi.new('unsigned int *') res = lib.posixmq_get(self._queue_id, buffer, size, priority, timeout) if res == lib.POSIXMQ_E_TIMEOUT: raise queue.Empty elif res != lib.POSIXMQ_OK: raise QueueError(res) data_size = size[0] data = ffi.buffer(buffer[0:data_size])[:] return pickle.loads(data)
def unlink(name): """ Remove a message queue *name*. """ q_name = ffi.new('char[]', name.encode('utf-8')) res = lib.posixmq_unlink(q_name) if res != lib.POSIXMQ_OK: raise QueueError(res)
def __init__(self, name, maxsize=10, maxmsgsize=1024): """ Constructor for message queue. *name* is an unique identifier of the queue, must starts with ``/``. *maxsize* is an integer that sets the upperbound limit on the number of items that can be placed in the queue (maximum value depends on system limit). *maxmsgsize* is a maximum size of the message in bytes (maximum value depends on hard system limit). """ queue_name = ffi.new('char[]', name.encode('utf-8')) queue_id = ffi.new('int *') res = lib.posixmq_open(queue_name, queue_id, maxmsgsize, maxsize) if res != lib.POSIXMQ_OK: raise QueueError(res) self._queue_id = queue_id[0] self._name = name self._maxsize = maxsize self._max_msg_size = maxmsgsize
def qattr(self): """ Return attributes of the message queue as a :class:`dict`: ``{'size': 5, 'max_size': 10, 'max_msgbytes': 1024}``. """ attr = ffi.new('struct mq_attr *') res = lib.posixmq_get_attr(self._queue_id, attr) if res != lib.POSIXMQ_OK: raise QueueError(res) return { 'size': attr.mq_curmsgs, 'max_size': attr.mq_maxmsg, 'max_msgbytes': attr.mq_msgsize, }