Implementation of thread 13 simple current limiter in Python

Official Python column article 50, students stop, don't miss this article starting from 0!

In the previous article, the academic committee proposed The idea of making current limiter based on Semaphore semaphore.

A simple summary is: dynamic release ensures a fixed number of available semaphores at any time.

We usually use semaphores like this

xuewei_semaphore = threading.Semaphore(4) #Application semaphore

#Use semaphores somewhere
xuewei_semaphore.acquire()

//do something here
....

xuewei_semaphore.release()

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

Because the 4 signal quota is set, up to 4 threads are allowed to run at the same time.

After obtaining more than 4 at any time, other threads can only wait, which is very similar to our inbound queue. When the security personnel see that there are too many people entering the queue, they stop the people behind them, know that the number of people waiting is reduced, and then release 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 PM, November 27, 2021
# @Author : LeiXueWei
# @CSDN/Juejin/Wechat: Thunder Science Committee
# @XueWeiTag: CodingDemo
# @File : threading_semephore.py
# @Project : hello
import threading
import time
import queue

xuewei_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'] -= 1


def 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 t in threads:
    t.join()

Here we apply for four empty slots for semaphores.

Then start 100 threads, keep getting semaphores, and then release them when they are finished.

At the same time, we have a buffer queue that only stores the current number of new arrivals.

By printing this waiting_ for_ According to the status of train, we can see that at most 4 people enter the queue at any time.

No more than four.

Operation effect

During the running process, we found that the size of the queue has always been 4

Finally, all the people who entered the station got on the bus, and the waiting people 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 4 metrics, so the maximum number of people who can enter the station and wait at each time is only 4.

It's like a subway stop.

We can also try to modify the code of inbound processing to the code below, and the readers can run it by themselves to see the effect.

xuewei_semaphore.acquire()
print(" %s go" % new_joiner)
waiting_for_train['value'] += 1
time.sleep(1)
waiting_for_train['value'] -= 1
print(" %s completed" % threading.current_thread().name)
xuewei_semaphore.release()

summary

OK, this current limiter is very simple. It's easy to show it in this intermediate programming.

Readers can copy the code, run it several times and think about it.

Later, the tutorial of UI development will be updated, and the school committee will write an interface to show this intuitively.

Programming is still fun. For those who like Python, please pay attention to the academic committee Python foundation column or Introduction to Python to master the big column

Continuous learning and continuous development, I'm Lei Xuewei!
Programming is very interesting. The key is to understand the technology thoroughly.
Welcome to wechat, like and support collection!

Keywords: Python Back-end

Added by saandel on Fri, 28 Jan 2022 00:14:42 +0200