Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python .Semaphore()和.BoundedSemaphore()之间有什么区别?_Python_Multithreading_Mutex_Semaphore_Python Multithreading - Fatal编程技术网

Python .Semaphore()和.BoundedSemaphore()之间有什么区别?

Python .Semaphore()和.BoundedSemaphore()之间有什么区别?,python,multithreading,mutex,semaphore,python-multithreading,Python,Multithreading,Mutex,Semaphore,Python Multithreading,我知道threading.Lock()等于threading.Semaphore(1) 也是threading.Lock()等于threading.BoundedSemaphore(1) 最近我遇到了threading.BoundedSemaphore(),它们之间有什么区别?例如以下代码段(用于对线程应用限制): 一个信号量可以被释放的次数超过它被获取的次数,这将使其计数器高于起始值。BoundedSemaphore必须高于起始值 from threading import Semaphore

我知道
threading.Lock()
等于
threading.Semaphore(1)

也是
threading.Lock()
等于
threading.BoundedSemaphore(1)

最近我遇到了
threading.BoundedSemaphore()
,它们之间有什么区别?例如以下代码段(用于对线程应用限制):


一个
信号量
可以被释放的次数超过它被获取的次数,这将使其计数器高于起始值。
BoundedSemaphore
必须高于起始值

from threading import Semaphore, BoundedSemaphore

# Usually, you create a Semaphore that will allow a certain number of threads
# into a section of code. This one starts at 5.
s1 = Semaphore(5)

# When you want to enter the section of code, you acquire it first.
# That lowers it to 4. (Four more threads could enter this section.)
s1.acquire()

# Then you do whatever sensitive thing needed to be restricted to five threads.

# When you're finished, you release the semaphore, and it goes back to 5.
s1.release()


# That's all fine, but you can also release it without acquiring it first.
s1.release()

# The counter is now 6! That might make sense in some situations, but not in most.
print(s1._value)  # => 6

# If that doesn't make sense in your situation, use a BoundedSemaphore.

s2 = BoundedSemaphore(5)  # Start at 5.

s2.acquire()  # Lower to 4.

s2.release()  # Go back to 5.

try:
    s2.release()  # Try to raise to 6, above starting value.
except ValueError:
    print('As expected, it complained.')    

线程模块提供简单的
信号量

Semaphore
提供了一个无界计数器,允许您调用
release()
任意次数的递增

但是,为了避免编程错误,使用
BoundedSemaphore
通常是正确的选择,如果
release()
调用试图将计数器增加到其最大大小之外,则会引发错误

编辑

信号量有一个内部计数器而不是锁标志(在锁的情况下),并且只有当超过给定数量的线程试图保持信号量时,它才会阻塞。根据信号量的初始化方式,这允许多个线程同时访问同一代码段

from threading import Semaphore, BoundedSemaphore

# Usually, you create a Semaphore that will allow a certain number of threads
# into a section of code. This one starts at 5.
s1 = Semaphore(5)

# When you want to enter the section of code, you acquire it first.
# That lowers it to 4. (Four more threads could enter this section.)
s1.acquire()

# Then you do whatever sensitive thing needed to be restricted to five threads.

# When you're finished, you release the semaphore, and it goes back to 5.
s1.release()


# That's all fine, but you can also release it without acquiring it first.
s1.release()

# The counter is now 6! That might make sense in some situations, but not in most.
print(s1._value)  # => 6

# If that doesn't make sense in your situation, use a BoundedSemaphore.

s2 = BoundedSemaphore(5)  # Start at 5.

s2.acquire()  # Lower to 4.

s2.release()  # Go back to 5.

try:
    s2.release()  # Try to raise to 6, above starting value.
except ValueError:
    print('As expected, it complained.')