Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple current limiter by Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge about how to achieve a simple current limiter in Python. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

To sum up: dynamic release ensures that there are a fixed number of semaphores available at any time.

We usually use semaphores like this.

Xuewei_semaphore = threading.Semaphore (4) # Application semaphore # use semaphore xuewei_semaphore.acquire () / / do something here....xuewei_semaphore.release () somewhere

The process of current limiting is actually the process of constantly using this limited semaphore.

Because the 4 signal limit is set, a maximum of 4 threads are allowed to run at the same time.

As long as you get more than 4 at any time, other threads can only wait, which is very similar to our queue. The security personnel saw that there were too many people entering the queue, stopped those at the back, knew that the number of people waiting was reduced, and then let some people into the waiting area of the station.

Go straight to the code and explain later. #! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 10:43 on 2021-11-27 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: threading_semephore.py# @ Project: helloimport threadingimport timeimport queuexuewei_semaphore = threading.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 time.sleep (1) print ("% s completed"% threading.current_thread (). Name) xuewei_semaphore.release () waiting_for_train ['value']-= 1def log_the_waiting_area_status (): while True: time.sleep (0.5) name = threading.current_thread () .name print ("name% s-size% s"% (name) Waiting_for_train ['value'])) q_watcher = threading.Thread (name= "waiting area", target=log_the_waiting_area_status) q_watcher.start () threads = [] for i in range (100): t_name = "t -" + str (I) t = threading.Thread (name=t_name, target=run) threads.append (t) t.start () for tin threads: t.join ()

Here we have applied for 4 empty slots of semaphore.

Then start 100 threads, constantly get the semaphore, and then release it.

At the same time, we have a buffer queue, which only stores the number of new entrants.

By printing the status of the waiting_for_train, we can see that only four people enter the queue at most at any time.

There won't be more than four.

Running effect

In the process of running, we found that the size of queue has always been 4. 5%.

At last, all the people who entered the station got on the bus, and the people waiting were cleared.

There are a total of 102 threads, one main thread, one waiting area status display thread, and another 100 threads, representing 100 inbound personnel.

Semaphore initializes four metrics, so the maximum number of people who can come in and wait at a time is only four.

Just like the subway intercepts entering the station.

We can also try to modify the inbound processing code to the following code, and the reader can run it to see the effect.

Xuewei_semaphore.acquire () print ("% s go"% new_joiner) waiting_for_train ['value'] + = 1time.sleep (1) waiting_for_train [' value']-= 1print ("% s completed"% threading.current_thread (). Name) xuewei_semaphore.release () these are all the contents of the article "how to achieve a simple current limiter in Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report