Set API: ¶
Module shared_atomic
-
set provide atomic operations, the set should be no longer than 8 bytes :attributes
encoding: readonly string character set
initial_length: readonly total length in bits
value: read/write value of the bytearray
int_value: readonly int value
size: readonly size of the set
mode: readonly ‘s’ for single process, ‘m’ for multiprocessing, on windows platform, only singleprocessing is supported
windows_unix_compatibility: readonly whether the source code should be compatible with windows x64
constructor to initialize the set, the set should be no longer than 8 bytes
- Parameters
-
-
initial – initial value of the set, if the initial value is longer than 8 bytes, please specify the trimming target length, or else it would fail.
-
mode – the mode in which the set will be shared. ‘singleprocessing’ or ‘s’ for single process, ‘multiprocessing’ or ‘m’ for multiprocessing, on windows platform, only singleprocessing is supported, setting it to ‘m’ or ‘multiprocessing’ will be ignored.
-
encoding – , character set, default ‘utf-8’
-
-
Switch between singleprocessing mode and multiprocessing mode, the function doesn’t exists on windows, since only single processing mode is supported on windows platform. the contents will be copied , other threads/processes would not be aware of the change.
- Parameters
-
newmode – the mode to change to, ‘m’ for multiproessing, ‘s’ for singleprocessing. default ‘m’
- Returns
-
None
-
Function to decode the bytes to set
- Parameters
-
-
bits_in_bytes – bytes needs to be decoded
-
encoding – character encoding
-
- Returns
-
the decoded set
-
function to encode the input_set with specific character encoding
- Parameters
-
-
input_set – input set
-
encoding – character encoding
-
- Returns
-
(data in integer representation, total length in bits)
-
Get the whole integer representation from the set, the whole set would be treated as a large integer
- Returns
-
the integer representation
-
Get the set atomically
- Returns
-
the set
-
same with get_int
-
Change the encoding of the string, if the original size is not enough, it will elongate the string, which is only valid in the master process/thread, if 8 bytes are not enough, it will fail.
- Parameters
-
newencode – new encoding, such as ‘utf-8’, ‘utf-16-le’
- Returns
-
None
-
Compare and set atomically, This compares the contents of self with the contents of i. If equal, the operation is a read-modify-write operation that writes n into self. If they are not equal, the operation is a read and the current contents of itself are written into i.
- Parameters
-
-
i – the set to be compared with
-
n – another bytes to be ready to set to self if comparision return True
-
- Returns
-
if self is equal to i return True, else return False
-
Compare and swap atomically, This compares the contents of self with the contents of n. If equal, the operation is a read-modify-write operation that writes n into self. If they are not equal, No operation is performed.
- Parameters
-
-
i – exchange value
-
n – The value to be compared with
-
- Returns
-
the initial value of self
-
Get and set atomically
- Parameters
-
data – new data set
- Returns
-
the original set
-
Set the whole integer representation from the set, the whole set would be treated as a large integer
- Returns
-
None
-
Set the value in the set, if the new data is longer than the original size of the set. it will expand the set accordingly which could only be used in the master process/thread the size of the set can be check with self.size
- Parameters
-
data – input set
- Returns
-
None
-
Value exchange between 3 pointers in 2 groups atomically, store i in itself after store itself in j
- Parameters
-
-
i – one atomic_set
-
j – another atomic_set
-
- Returns
-
None
-
Atomically store contents from another set to the this set, if the other set is different with this one in size , the function will fail.
- Parameters
-
i – another set to store its value to self
- Returns
-
None
-
Function to finish the shift and add of the data prefix
- Parameters
-
-
data_prefix – integer for the data_prefix
-
accumulate_length – total data length in bits accumulated so far
-
input_length – length of the data segment in bits
-
kind – sorts of data
-
- Returns
-
(data_prefix, accumulate_length)
-
same with get_set and set_set