gpt4 book ai didi

python - multiprocessing.Semaphore 和 multiprocessing.BoundedSemaphore 有什么区别?

转载 作者:太空宇宙 更新时间:2023-11-04 05:02:06 27 4
gpt4 key购买 nike

multiprocessing.BoundedSemaphore(3)multiprocessing.Sempahore(3) 有何不同?

我希望 multiprocessing.BoundedSemaphore(3) 永远不会允许其内部计数器值超过 3。

由此我得出结论,即使我获取此信号量的进程最终错误地多次释放信号量(比如获取一次但释放五次),它也不会允许超过 3 个进程获取信号量信号量在任何时间点。然而,我的这个结论似乎是不正确的。

这是我的 Python 示例代码,位于名为 multi.py 的文件中。

#!/usr/bin/env python
import multiprocessing
import time

def f(i, sem):
print 'f(%d) acquiring ...' % i
sem.acquire()
print 'f(%d) acquired' % i

time.sleep(i + 1)

print 'f(%d) releasing ...' % i
sem.release()
sem.release() # Extra release on purpose
sem.release() # Extra release on purpose
sem.release() # Extra release on purpose
sem.release() # Extra release on purpose
print 'f(%d) released' % i

processes = []

sem = multiprocessing.BoundedSemaphore(3)
for i in range(10):
p = multiprocessing.Process(target=f, args=(i, sem))
p.start()
processes.append(p)

for p in processes:
p.join()

print 'Done'

这是我的 macOS Sierra 10.12.5 和 Python 2.7.13 的输出。下面输出之间的空行是我手动插入的,以指示输出之间的暂停。

$ ./multi.py                                                                                                                                                                            [56/1853]
f(0) acquiring ...
f(0) acquired
f(1) acquiring ...
f(1) acquired
f(2) acquiring ...
f(2) acquired
f(3) acquiring ...
f(4) acquiring ...
f(5) acquiring ...
f(6) acquiring ...
f(7) acquiring ...
f(8) acquiring ...
f(9) acquiring ...

f(0) releasing ...
f(0) released
f(3) acquired
f(5) acquired
f(4) acquired
f(6) acquired
f(7) acquired

f(1) releasing ...
f(1) released
f(9) acquired
f(8) acquired

f(2) releasing ...
f(2) released

f(3) releasing ...
f(3) released

f(4) releasing ...
f(4) released

f(5) releasing ...
f(5) released

f(6) releasing ...
f(6) released

f(7) releasing ...
f(7) released

f(8) releasing ...
f(8) released

f(9) releasing ...
f(9) released
Done

上面的输出表明f(0)f(1)f(2)获取了有界信号量和剩余的进程在尝试获取指示有界信号量的内部计数器现在已降至 0 时阻塞。

然后 f(0) 释放有界信号量 5 次。在此之后,我期望有界信号量的内部计数器为 3(因为它是有界的),所以现在应该还有 3 个进程能够获取有界信号量。但是从输出来看,有界信号量的内部计数器似乎已增加到 5,因为有 5 个进程获取了有界信号量。获取有界信号量的 5 个进程是 f(3)f(5)f(4)f (7)f(7)

如果 multiprocessing.BoundedSemaphore 对象的内部计数器有可能超过初始值,那么它与 multiprocessing.Semaphore 有何不同?

最佳答案

来自关于 BoundedSemaphore 的 Python 2.7 文档:

Note On Mac OS X, this is indistinguishable from Semaphore because sem_getvalue() is not implemented on that platform.

https://docs.python.org/2/library/multiprocessing.html#synchronization-primitives

关于python - multiprocessing.Semaphore 和 multiprocessing.BoundedSemaphore 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45459276/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com