### POSIX Message Queue Example Source: https://pythonhosted.org/ipcqueue Demonstrates creating, putting messages with priorities, getting messages, and cleaning up a POSIX message queue. Ensure the queue name starts with '/'. ```python >>> from ipcqueue import posixmq >>> q = posixmq.Queue('/foo') >>> q.qsize() 0 >>> q.put([1, 'A']) >>> q.put([2, 'B'], priority=2) >>> q.put([3, 'C'], priority=0) >>> q.qsize() 3 >>> q.get() [2, 'B'] >>> q.get() [1, 'A'] >>> q.get() [3, 'C'] >>> q.close() >>> q.unlink() ``` -------------------------------- ### POSIX Message Queue Operations Source: https://pythonhosted.org/ipcqueue/_sources/index.rst.txt Demonstrates creating, putting messages with priorities, getting messages, and cleaning up a POSIX message queue. Ensure the queue path is valid and accessible. ```python from ipcqueue import posixmq q = posixmq.Queue('/foo') q.qsize() 0 q.put([1, 'A']) q.put([2, 'B'], priority=2) q.put([3, 'C'], priority=0) q.qsize() 3 q.get() [2, 'B'] q.get() [1, 'A'] q.get() [3, 'C'] q.close() q.unlink() ``` -------------------------------- ### Queue.get_nowait() Source: https://pythonhosted.org/ipcqueue Get and return an item from queue, equivalent to `get(block=False)`. ```APIDOC get_nowait(_msg_type=0_) Get and return an item from queue, equivalent to `get(block=False)`. _msg_type_ specifies the type of requested message. If it’s `0`, then the first message in the queue is read, if it’s greater than `0`, then the first message in the queue of requested type is read and if it’s less than `0`, then the first message in the queue with the lowest type less than or equal to the absolute value of _msg_type_ will be read. ``` -------------------------------- ### Basic SYS V Message Queue Operations Source: https://pythonhosted.org/ipcqueue Demonstrates the basic usage of the `ipcqueue.sysvmq.Queue` class, including creating a queue, putting items with and without specific message types, checking the queue size, retrieving items, and closing the queue. ```python >>> from ipcqueue import sysvmq >>> q = sysvmq.Queue(1) >>> q.qsize() >>> q.put([1, 'A']) >>> q.put([2, 'B'], msg_type=2) >>> q.put([3, 'C'], msg_type=2) >>> q.put([4, 'D'], msg_type=1) >>> q.qsize() 4 >>> q.get(msg_type=2) [2, 'B'] >>> q.get() [1, 'A'] >>> q.get() [3, 'C'] >>> q.get() [4, 'D'] >>> q.close() ``` -------------------------------- ### Queue.qsize() Source: https://pythonhosted.org/ipcqueue Return the approximate size of the queue. ```APIDOC qsize() Return the approximate size of the queue. Note, `qsize() > 0` doesn’t guarantee that a subsequent `get()` will not block. ``` -------------------------------- ### Queue.qattr() Source: https://pythonhosted.org/ipcqueue Return attributes of the message queue. ```APIDOC qattr() Return attributes of the message queue as a `dict`: `{'size': 3, 'max_bytes': 8192}`. ``` -------------------------------- ### SYS V Message Queue Operations Source: https://pythonhosted.org/ipcqueue/_sources/index.rst.txt Illustrates the usage of SYS V message queues, including creating a queue, putting messages with types, retrieving messages by type or in FIFO order, and closing the queue. Note that SYS V queues use integer IDs. ```python from ipcqueue import sysvmq q = sysvmq.Queue(1) q.qsize() q.put([1, 'A']) q.put([2, 'B'], msg_type=2) q.put([3, 'C'], msg_type=2) q.put([4, 'D'], msg_type=1) q.qsize() 4 q.get(msg_type=2) [2, 'B'] q.get() [1, 'A'] q.get() [3, 'C'] q.get() [4, 'D'] q.close() ``` -------------------------------- ### Queue.get() Source: https://pythonhosted.org/ipcqueue Remove and return an item from the queue. ```APIDOC get(_block=True_, _msg_type=0_) Remove and return an item from the queue. If _block_ argument is `True`, block if necessary until an item is available. Otherwise, return an item if one is immediately available, else raise the `queue.Empty` exception. _msg_type_ specifies the type of requested message. If it’s `0`, then the first message in the queue is read, if it’s greater than `0`, then the first message in the queue of requested type is read and if it’s less than `0`, then the first message in the queue with the lowest type less than or equal to the absolute value of _msg_type_ will be read. ``` -------------------------------- ### Queue Class Source: https://pythonhosted.org/ipcqueue Represents a SYS V message queue. ```APIDOC class ipcqueue.sysvmq.Queue(_key=None_, _max_bytes=None_) SYS V message queue. Constructor for message queue. _key_ is an unique identifier of the queue, must be positive number or `0` for private queue. _max_bytes_ is a maximum number of bytes allowed in queue (maximum value depends on hard system limit). ``` -------------------------------- ### Queue.put() Source: https://pythonhosted.org/ipcqueue Put _item_ into the queue. ```APIDOC put(_item_, _block=True_, _msg_type=1_, _pickle_protocol=1_) Put _item_ into the queue. If _block_ is `True`, block if necessary until a free slot is available. Otherwise, put an _item_ on the queue if a free slot is immediately available, else raise the `queue.Full` exception. _msg_type_ must be positive integer value, this value can be used by the receiving process for message selection. _pickle_protocol_ is a format used by Python’s `pickle`. ``` -------------------------------- ### Queue.put_nowait() Source: https://pythonhosted.org/ipcqueue Put _item_ into the queue, equivalent to `put(item, block=False)`. ```APIDOC put_nowait(_item_, _msg_type=1_, _pickle_protocol=1_) Put _item_ into the queue, equivalent to `put(item, block=False)`. _msg_type_ must be positive integer value, this value can be used by the receiving process for message selection. _pickle_protocol_ is a format used by Python’s `pickle`. ``` -------------------------------- ### ipcqueue.posixmq.QueueError Source: https://pythonhosted.org/ipcqueue Custom exception for POSIX queue errors. ```APIDOC ## exception ipcqueue.posixmq.QueueError(errno, msg=None) ### Description Indicates a Queue error. Contains additional attributes `errno` and `msg`. ### Attributes - **errno** (int) - System-dependent error code. - **msg** (str) - Error message. ### Constants - **QueueError.ERROR** - **QueueError.INVALID_VALUE** - **QueueError.NO_PERMISSIONS** - **QueueError.NO_SYSTEM_RESOURCES** - **QueueError.INVALID_DESCRIPTOR** - **QueueError.INTERRUPTED** - **QueueError.TOO_BIG_MESSAGE** - **QueueError.TIMEOUT** - **QueueError.DOES_NOT_EXIST** ``` -------------------------------- ### QueueError Source: https://pythonhosted.org/ipcqueue Indicates a Queue error. Contains additional attributes _errno_ and _msg_. ```APIDOC exception ipcqueue.sysvmq.QueueError(_errno_, _msg=None_) Indicates Queue error. Contains additional attributes _errno_ and _msg_. Value of the _errno_ is system dependent, do don’t use numeric codes directly, use constants **QueueError.ERROR**, **QueueError.INVALID_VALUE**, **QueueError.NO_PERMISSIONS**, **QueueError.NO_SYSTEM_RESOURCES**, **QueueError.INVALID_DESCRIPTOR**, **QueueError.INTERRUPTED** and **QueueError.TOO_BIG_MESSAGE**. ``` -------------------------------- ### Queue.close() Source: https://pythonhosted.org/ipcqueue Close a message queue. ```APIDOC close() Close a message queue. ``` -------------------------------- ### ipcqueue.posixmq.Queue Source: https://pythonhosted.org/ipcqueue Represents a POSIX message queue for inter-process communication. ```APIDOC ## class ipcqueue.posixmq.Queue(name, maxsize=10, maxmsgsize=1024) ### Description POSIX message queue implementation. Allows processes to communicate by sending and receiving messages. ### Parameters #### Path Parameters - **name** (string) - Required - An unique identifier of the queue, must start with `/`. - **maxsize** (integer) - Optional - The upperbound limit on the number of items that can be placed in the queue. Defaults to 10. - **maxmsgsize** (integer) - Optional - The maximum size of a message in bytes. Defaults to 1024. ### Methods #### close() Close a message queue. #### get(block=True, timeout=None) Remove and return an item from the queue. Supports blocking with optional timeout. - **block** (boolean) - Optional - If True, block until an item is available. Defaults to True. - **timeout** (float) - Optional - Maximum time in seconds to block. If None, blocks indefinitely. #### get_nowait() Get and return an item from queue, equivalent to `get(block=False)`. #### put(item, block=True, timeout=None, priority=0, pickle_protocol=1) Put an item into the queue. Supports blocking with optional timeout and priority. - **item** (any) - Required - The item to put into the queue. Will be pickled. - **block** (boolean) - Optional - If True, block until a free slot is available. Defaults to True. - **timeout** (float) - Optional - Maximum time in seconds to block. If None, blocks indefinitely. - **priority** (integer) - Optional - Priority of the message. Higher values are retrieved first. Defaults to 0. - **pickle_protocol** (integer) - Optional - The pickle protocol version to use. Defaults to 1. #### put_nowait(item, priority=0, pickle_protocol=1) Put an item into the queue without blocking, equivalent to `put(item, block=False)`. - **item** (any) - Required - The item to put into the queue. Will be pickled. - **priority** (integer) - Optional - Priority of the message. Higher values are retrieved first. Defaults to 0. - **pickle_protocol** (integer) - Optional - The pickle protocol version to use. Defaults to 1. #### qattr() Return attributes of the message queue as a dictionary. #### qsize() Return the approximate size of the queue. #### unlink() Remove a message queue. This is an instance method. ### Example ```python from ipcqueue import posixmq # Create a queue q = posixmq.Queue('/my_ipc_queue', maxsize=100) # Put an item with priority q.put('hello', priority=5) # Get an item message = q.get() print(message) # Close and unlink the queue q.close() q.unlink() ``` ``` -------------------------------- ### ipcqueue.posixmq.unlink Source: https://pythonhosted.org/ipcqueue Removes a message queue by its name. This is a class method. ```APIDOC ## unlink(name) ### Description Remove a message queue by its unique name. ### Parameters #### Path Parameters - **name** (string) - Required - The unique name of the message queue to remove. Must start with '/'. ### Request Example ```python from ipcqueue import posixmq posixmq.unlink('/my_queue') ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.