Home > Backend Development > Python Tutorial > Introduction to semaphores and bounded semaphores under Python threads

Introduction to semaphores and bounded semaphores under Python threads

不言
Release: 2018-09-11 16:28:05
Original
1034 people have browsed it

This article brings you an introduction to semaphores and bounded semaphores under Python threads. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Semaphores and bounded semaphores

The semaphore is a counter-based synchronization primitive. Each time the acquire() method is called, the counter is decremented by 1, and each time release() is called ) method, this counter is incremented by 1. If the counter is 0, the acquire() method will block until another thread calls the release() method.

Semaphore

Syntax:
s=Semqphore(value)
Copy after login

Create a new semaphore. value is the initial value of the counter. If value is omitted, it will default to 1

Common methods:

s.acquire(blocking): Get the semaphore. If the internal counter is greater than 0, this method will decrement its value by 1.
Then return immediately. If its value is 0, this method blocks until another thread calls the release() method.
The behavior of the blocking parameter is the same as described for Lock and RLock objects.

s.release(): Release a semaphore by adding 1 to the internal counter value.
If the counter is 0 and another thread is waiting, the thread will be awakened.
If multiple threads are waiting, only one can be returned from its acquire() call, and the order is undefined.

Bounded semaphore

Syntax:
bs=BoundedSemaphore(value)
Copy after login

Create a new bounded semaphore. value is the initial value of the counter. If omitted, it will default to 1.

The difference between semaphores and bounded semaphores

BoundedSemaphore works exactly the same as Semaphore, but release() The number of operations cannot exceed the number of acquire() operations.

Subtle differences between semaphores and mutexes

Semaphores can be used to send signals. For example, the acquire() and release() methods can be called from different threads to communicate between the producer and consumer threads.

Related recommendations:

Detailed explanation of the use of semaphores in Python3.X threads

python Semaphore (Semaphore) What is it? (detailed examples)

The above is the detailed content of Introduction to semaphores and bounded semaphores under Python threads. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template