原子列表API

模块shared_atomic

class shared_atomic.atomic_list.atomic_list(initial: Iterable, encoding='utf-8', mode='singleprocessing')

列表提供原子操作,列表必须不超过8字节

构造函数提供原子操作,列表必须不超过8字节

参数
  • initial – 列表的初始值,如果初始值长于8字节,请指定截短的目标字节数,否则会失败

  • mode – 列表的共享模式,’singleprocessing’或者’s’是单进程模式,’multiprocessing’或者’m’是多进程模式在windows平台上只支持单进程模式,设成’multiprocessing’或者’m’会被忽略。

  • encoding – 字符集,默认值’utf-8’

change_mode(newmode='m')

在单进程与多进程模式之间切换,原来的内容会被复制到新的共享数组中,因为windows平台上只有单进程模式,这个函数在windows上并不存在,其它进程或者线程无法感知这种改变

参数

newmode – 需要切换的模式,’m’ 或者 ‘multiprocessing’ 是多进程模式, ‘s’ or ‘singleprocessing’单进程模式. 默认值是多进程模式

返回

decode(bits_in_bytes: bytes) list

将存储的字节解码成列表

参数
  • bits_in_bytes – 需要解码的字节

  • encoding – 字符集

返回

解码后的列表

encode(input_list: list) -> (<class 'int'>, <class 'int'>)

用具体的字符集编码input_list

参数
  • input_list – 输入列表

  • encoding – 字符集

返回

(用整数编码的整数, 用比特表示的字符长度)

get_int() int

从列表获取整个整数表示, 整个列表表示为大整数

返回

整数表示

get_list() list

原子的获取这个列表

返回

列表

property int_value: int

与get_int一致

list_compare_and_set(i, n: list) bool

原子比较和设置,比较自身和i的内容,如果相等,这个过程就是读-修改-写的操作,将n的内容写入自身,如果不相等,是只读操作,自己的内容写入i。

参数
  • i – 需要比较的列表

  • n – another bytes to be ready to write to self if comparision return True

返回

当自身与i相等时就返回真,否则就返回错误。

list_get_and_set(data: list) list

原子获取列表

参数

data – 新数据列表

返回

原来的列表

list_shift(i, j)

3个指针之间两两交换值,将自己的值存入j然后将i存入自身

参数
  • i – 一个原子列表

  • j – 另一个原子列表

返回

list_store(i)

Atomically store contents from another list to the this list, if the other list is different with this one in size , the function will fail. You can use .size to check the space it use.

参数

i – 将要存储值的另一个列表

返回

reencode(newencode: str)

Change the encoding of the string in the list, if the original size is not enough, it will elongate the string, if 8 bytes are not enough, it will fail.

参数

newencode – 新编码,像是’utf-8’,’utf-16-le’

返回

set_int(integer: int)

以整数表示来设置这个列表,整个列表将被当成一个大的整数。

返回

set_list(data: list)

设置这个列表的值,如果长于列表的原始长度,它需要扩展,就会失掉原子性,可以通过self.size来检查空间占用。

参数

data – 输入列表

返回

shift_and_add(data_prefix: int, accumulate_length: int, input_length: int, kind: int) -> (<class 'int'>, <class 'int'>)

Function to finish the shift and add of the data prefix

参数
  • 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

返回

(data_prefix, accumulate_length)

property value: list

与get_list和set_list一致