pysyncq package
Subpackages
Submodules
pysyncq.header module
Collects important constants together into a common namespace.
- exception pysyncq.header.ScreenedMessage[source]
Bases:
ExceptionUsed to signal when a message has been read from the queue and then screened from further use because the sender or type reside in the instance’s screen sets.
- class pysyncq.header.qset(iterable=())[source]
Bases:
setsqet( [iterable] )
A sub-class of set, this differs only in the way that every element is first converted to a byte string before being added to the set. The reason being that messages are written and read from the PySyncQ shared memory as bytes. To screen new messages by sender or message type, then, one only need to compare the raw byte string taken from the queue body against the qsets that store screened sender and type byte strings.
pysyncq.pysyncq module
Python Synchronisation Queue
- class pysyncq.pysyncq.PySyncQ(name=None, create=True, size=4096, start=None)[source]
Bases:
object- class pysyncq.PySyncQ( name = None , create = True , size = <Page Size> ,
start = None )
Creates a synchronisation queue. name is a str that names the shared memory that is the backbone of the queue, and to which all processes will connect. create is a bool that signals whether to create new shared memory (True) or to use existing shared memory (False). size is an int of 0 or greater giving the number of bytes to request for the shared memory. start names the start method that will be used to create child processes. Hence, this must be a valid start method string as returned by the multiprocessing module’s get_all_start_methods(). If start is None then multiprocessing’s get_start_method() is called to determine the start method string.
Each process that wishes to read/write on the queue must make a separate call to the .open( ) method, in order to register itself with the queue as a unique reader/writer.
- append(self, msgtype='', msg='', block=False, timer=0.5)[source]
Adds a new message to the tail of the queue. The message header stores the sender name and msgtype as message type. msg forms the main body of the message. If msgtype or msg are not already str or bytes then they are first cast to str before casting to bytes with the default encoding.
If the queue lacks sufficient free space in which to write the message header and body then a MemoryError exception is raised, unless block is True. Then append will wait until there is enough room in the queue.
append will wait indefinitely for free space if timer is None. But timer can be a float that specifies the number of seconds to wait for. If the timer expires before the message is appended to the queue then the MemoryError exception is raise.
- open(sender=None, filtself=True)[source]
open( sender = pid , filtself = True ) registers the current process with the queue. sender is a string naming the process in each message that it sends; if set to None, then the current process ID i.e. pid is used as the sender (default). The bool filtself says whether the sender string is automatically added to the scrnsend set; default is True.
- pop(block=False, timer=0.5, decode=True)[source]
Reads the next next unread message from the queue and returns the tuple ( sender , type , msg ) … see append. If the sender or type string is found in the scrnsend or scrntype sets, respectively, then the message is skipped, and pop looks for the next unread message in the queue. If there are no unread and unscreened messages then None is returned, unless block is True.
Then pop will wait until there is a new message to read. Pop will wait indefinitely if timer is None. Otherwise, timer can be a float that gives the number of seconds that pop will wait for. If the timer expires before an unread message becomes available then None will be returned.
By default, messages are decoded from bytes to str with the default encoding. But if decode is False then the raw bytes are returned.