Set API:

Module shared_atomic

class shared_atomic.atomic_set. atomic_set ( initial : Iterable , encoding = 'utf-8' , mode = 'singleprocessing' , windows_unix_compatibility : bool = False )

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’

change_mode ( newmode = 'm' )

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

decode ( bits_in_bytes : bytes ) set

Function to decode the bytes to set

Parameters
  • bits_in_bytes – bytes needs to be decoded

  • encoding – character encoding

Returns

the decoded set

encode ( input_set : 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_int ( ) size_t

Get the whole integer representation from the set, the whole set would be treated as a large integer

Returns

the integer representation

get_set ( ) set

Get the set atomically

Returns

the set

property int_value : size_t

same with get_int

reencode ( newencode : unicode )

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

set_compare_and_set ( i , n : set ) bool

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

set_compare_and_set_value ( i : set , n : set ) set

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

set_get_and_set ( data : set ) set

Get and set atomically

Parameters

data – new data set

Returns

the original set

set_int ( integer : size_t )

Set the whole integer representation from the set, the whole set would be treated as a large integer

Returns

None

set_set ( data : set )

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

set_shift ( i , j )

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

set_store ( i )

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

shift_and_add ( data_prefix : size_t , accumulate_length : char , input_length : char , kind : char )

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)

property value : set

same with get_set and set_set