Python SemaphoreIn the following tutorial, we will understand the multi-threading synchronization with the help of Semaphore in Python. Let us begin with understanding Python Semaphore. Understanding the Semaphore
Now let us understand the semaphores in the Python programming language. Understanding the Python Semaphores
Let us consider the following syntax in order to create an object of Semaphore. Syntax:Explanation: In the above syntax, the object_name is the object of the Semaphore class. The ‘count’ parameter of the Semaphore class is the number of Threads allowed to access simultaneously. The default value of this parameter is 1. Whenever the acquire() function is executed by a Thread, the value of the “count” parameter will be decremented by one. Whenever the release() function is executed by a Thread, the value of the “count” parameter will be incremented by one. This statement implies that whenever we call the acquire() method, the “count” parameter value will be decremented, whereas on calling the release() method, the “count” parameter value will be incremented. Methods of creating a Semaphore objectCase 1: In the following case, we do not specify the argument within the Semaphore class while creating an object. Thus, the value of the count variable is 1 because of which only a thread is permitted to access. This case is the exact copy of the Lock concept. The syntax for the same is shown below: Syntax: Case 2: In the following case, an object of the Semaphore class can be accessed by ‘n‘ Threads at a time. The remaining Threads have to wait until releasing the semaphore. The syntax for the same is shown below: Syntax: Let us consider the following example to understand the complete concept properly. Example: Output Javatpoint, Javatpoint, Javatpoint, Javatpoint, Thread 1 Thread 3 Thread 4 Thread 2 Javatpoint, Javatpoint, Javatpoint, Javatpoint, Javatpoint, Javatpoint, Thread 1 Thread 5 Javatpoint, Javatpoint, Thread 6 Thread 3 Thread 4 Javatpoint, Javatpoint, Thread 2 Javatpoint, Javatpoint, Thread 5 Javatpoint, Thread 1 Javatpoint, Thread 3 Javatpoint, Thread 6 Thread 2 Thread 4 Javatpoint, Javatpoint, Javatpoint, Thread 5 Javatpoint, Thread 1 Javatpoint, Thread 3 Javatpoint, Thread 6 Thread 2 Javatpoint, Javatpoint, Thread 4 Javatpoint, Thread 5 Javatpoint, Thread 1 Thread 3 Javatpoint, Javatpoint, Thread 2 Thread 6 Javatpoint, Javatpoint, Thread 4 Javatpoint, Thread 5 Javatpoint, Thread 1 Thread 3 Thread 2 Thread 6 Javatpoint, Thread 4 Thread 5 Thread 6 Explanation: In the above snippet of code, we have imported the required modules and created an object for the Semaphore class with a count value of 4. We have then defined a function using the acquire() function on the object. We have then used the for-loop to iterate the value to 6. We have then called the release() function and created multiple threads. At last, we have called the threads using the start() function. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/courses/263434.html