To summarize: dynamic release, ensuring that a fixed number of semaphores are available at any given time.
We usually use semaphores like this
xuewei_semaphore = (4) #Request a semaphore # Use a semaphore somewhere xuewei_semaphore.acquire() //do something here .... xuewei_semaphore.release()
The process of current limiting is really the process of constantly using this finite amount of signals.
Because of the 4 signal quota set, up to 4 threads are allowed to run at the same time.
Arbitrary time as long as the acquisition of more than 4 after the other thread can only wait, this is very similar to us into the station queue. The security officer sees too many people entering the queue, stops the ones behind them, knows that the number of people waiting is reduced, and then releases some more people into the waiting area of the station.
Let's go straight to the code and explain it later.
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/27 10:43 PM # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Lei Xuewei # @XueWeiTag: CodingDemo # @File : threading_semephore.py # @Project : hello import threading import time import queue xuewei_semaphore = (4) print("xuewei_semaphore:", xuewei_semaphore) waiting_for_train = {"value": 0} def run(): new_joiner = threading.current_thread().name # print(" %s ready" %new_joiner ) xuewei_semaphore.acquire() print(" %s go" % new_joiner) waiting_for_train['value'] += 1 (1) print(" %s completed" % threading.current_thread().name) xuewei_semaphore.release() waiting_for_train['value'] -= 1 def log_the_waiting_area_status(): while True: (0.5) name = threading.current_thread().name print("name %s - size %s " % (name, waiting_for_train['value'])) q_watcher = (name="waiting area", target=log_the_waiting_area_status) q_watcher.start() threads = [] for i in range(100): t_name = "t-" + str(i) t = (name=t_name, target=run) (t) () for t in threads: ()
Here we have requested the semaphore 4 empty slots.
Then start 100 threads that keep getting semaphores and then release them when they're done.
Also we have a buffer queue that only holds the currentnew inbound stationThe number of people.
By printing the status of this waiting_for_train, we can see that at most 4 people are in the queue at any given moment.
And no more than four.
operational effect
During the run, we find that the size of the queue is always 4.
Eventually all the incoming people are in the station and on the train, and the waiting list clears.
There are a total of 102 threads here, a main thread, a waiting area status display thread, and another hundred threads representing the 100 inbound people.
The semaphore is initialized with 4 metrics, so the maximum number of people who can enter the station and wait at a time is at most 4.
Same as the subway stop entry.
We can also try to change the code for inbound processing to the code below, and the reader can run it on their own to see the effect.
xuewei_semaphore.acquire() print(" %s go" % new_joiner) waiting_for_train['value'] += 1 (1) waiting_for_train['value'] -= 1 print(" %s completed" % threading.current_thread().name) xuewei_semaphore.release()
summarize
Okay, this current limiter is very simple, and the supporting programming in this intermediate level simply brings it up to speed.
Readers can copy the code, run it a few times, and think about it.
To this article on the Python implementation of a simple flow limiter introduction to this article, more related Python flow limiter content, please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!