Python 中的原子操作是什么?
What is Atomic Operations in Python?
我是 Python 的初学者。现在我阅读了有关线程的内容,但我有一些问题。
IMO 原子操作 (AO) = 最简单的操作。 dev中最简单的操作是a = 1
。但我确实阅读了这篇文章 (http://preshing.com/20130618/atomic-vs-non-atomic-operations/),我的印象是它不是最简单的 operation\AO。作者告诉我们这个操作分为两个操作,这个操作不是AO。这个操作是 AO 它必须有另一种类型。但我必须说他讲述了 C/C++ 和字节码,我认为在 Python 中也是如此。我如何理解这取决于类型和编译器。但是 Python 是动态类型的语言。它没有类型。
我决定向社区询问这些问题:
Python中的原子操作是什么?
Python中AO有哪些操作?
如果简单的操作不简单那么我不明白什么是原子操作
Python 文档的常见问题解答似乎对 What kinds of global value mutation are thread-safe?
下的这个问题有很好的回答
我希望您理解本地堆栈变量或特定线程专用的变量(不会有线程安全问题)与全局变量或共享变量之间的区别。
我开发了一个包 shared_atomic,其性能优于多处理和多线程 lock/Rlock。
该模块可用于多进程多线程条件下的原子操作
Environment,
LINUX/MacOSX
CPython 3.0 - 3.11
Pypy 3.0 - 3.9
Windows
CPython 3.0 - 3.11
Pypy not supported
Included Datatypes:
atomic_int
atomic_uint
atomic_float
atomic_bool
atomic_bytearray
atomic_string
atomic_set, package bitarray >= 2.4.0 is needed.
atomic_list, package bitarray >= 2.4.0 is needed.
Requirement,
LINUX/MacOSX
the package requires libatomic installed on Linux platform
cffi >= 1.0.0 <= 1.1.15 to compile through on Linux/MacOSX platform
gcc >= 4.8 to include the atomic APIs.
Windows
cppyy >= 1.5.0 <=2.3.1
only support single thread/multiple threads modes, multiprocessing mode is not supported on windows
Installation
To install shared_atomic, use pip:
pip install shared_atomic
Usage
create function used by child processes, refer to UIntAPIs, IntAPIs, BytearrayAPIs, StringAPIs, SetAPIs, ListAPIs, in each process, you can create multiple threads.
def process_run(a):
def subthread_run(a):
a.array_sub_and_fetch(b'x0F')
threadlist = []
for t in range(5000):
threadlist.append(Thread(target=subthread_run, args=(a,)))
for t in range(5000):
threadlist[t].start()
for t in range(5000):
threadlist[t].join()
create the shared bytearray
a = atomic_bytearray(b'ab', length=7, paddingdirection='r', paddingbytes=b'012', mode='m')
start processes/threads to utilize the shared bytearray
processlist = []
for p in range(2):
processlist.append(Process(target=process_run, args=(a,)))
for p in range(2):
processlist[p].start()
for p in range(2):
processlist[p].join()
assert a.value == int.to_bytes(27411031864108609,length=8,byteorder='big')
Currently concurrent.futures.ProcessPoolExecutor and concurrent.futures.ThreadPoolExecutor cannot be used to start multiple executions ,for serialization problems
源代码回购:
https://github.com/irvinren/shared_atomic.git
https://gitee.com/irvinren/shared_atomic.git
有关文档,请转到:
https://shared-atomic.readthedocs.io/en/latest/
该项目已获得 GPL v3.0 许可
我是 Python 的初学者。现在我阅读了有关线程的内容,但我有一些问题。
IMO 原子操作 (AO) = 最简单的操作。 dev中最简单的操作是a = 1
。但我确实阅读了这篇文章 (http://preshing.com/20130618/atomic-vs-non-atomic-operations/),我的印象是它不是最简单的 operation\AO。作者告诉我们这个操作分为两个操作,这个操作不是AO。这个操作是 AO 它必须有另一种类型。但我必须说他讲述了 C/C++ 和字节码,我认为在 Python 中也是如此。我如何理解这取决于类型和编译器。但是 Python 是动态类型的语言。它没有类型。
我决定向社区询问这些问题:
Python中的原子操作是什么?
Python中AO有哪些操作?
如果简单的操作不简单那么我不明白什么是原子操作
Python 文档的常见问题解答似乎对 What kinds of global value mutation are thread-safe?
下的这个问题有很好的回答我希望您理解本地堆栈变量或特定线程专用的变量(不会有线程安全问题)与全局变量或共享变量之间的区别。
我开发了一个包 shared_atomic,其性能优于多处理和多线程 lock/Rlock。 该模块可用于多进程多线程条件下的原子操作
Environment,
LINUX/MacOSX
CPython 3.0 - 3.11
Pypy 3.0 - 3.9
Windows
CPython 3.0 - 3.11
Pypy not supported
Included Datatypes:
atomic_int
atomic_uint
atomic_float
atomic_bool
atomic_bytearray
atomic_string
atomic_set, package bitarray >= 2.4.0 is needed.
atomic_list, package bitarray >= 2.4.0 is needed.
Requirement,
LINUX/MacOSX
the package requires libatomic installed on Linux platform
cffi >= 1.0.0 <= 1.1.15 to compile through on Linux/MacOSX platform
gcc >= 4.8 to include the atomic APIs.
Windows
cppyy >= 1.5.0 <=2.3.1
only support single thread/multiple threads modes, multiprocessing mode is not supported on windows
Installation
To install shared_atomic, use pip:
pip install shared_atomic
Usage
create function used by child processes, refer to UIntAPIs, IntAPIs, BytearrayAPIs, StringAPIs, SetAPIs, ListAPIs, in each process, you can create multiple threads.
def process_run(a):
def subthread_run(a):
a.array_sub_and_fetch(b'x0F')
threadlist = []
for t in range(5000):
threadlist.append(Thread(target=subthread_run, args=(a,)))
for t in range(5000):
threadlist[t].start()
for t in range(5000):
threadlist[t].join()
create the shared bytearray
a = atomic_bytearray(b'ab', length=7, paddingdirection='r', paddingbytes=b'012', mode='m')
start processes/threads to utilize the shared bytearray
processlist = []
for p in range(2):
processlist.append(Process(target=process_run, args=(a,)))
for p in range(2):
processlist[p].start()
for p in range(2):
processlist[p].join()
assert a.value == int.to_bytes(27411031864108609,length=8,byteorder='big')
Currently concurrent.futures.ProcessPoolExecutor and concurrent.futures.ThreadPoolExecutor cannot be used to start multiple executions ,for serialization problems
源代码回购:
https://github.com/irvinren/shared_atomic.git
https://gitee.com/irvinren/shared_atomic.git
有关文档,请转到:
https://shared-atomic.readthedocs.io/en/latest/
该项目已获得 GPL v3.0 许可