Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use redis+python to make message queue

Shulou Source: shulou.com Published: 2022-06-01 02:52:14 09月25日 Update

This article mainly explains "how to use redis+python to do message queue", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use redis+python to do message queue"!

First, a brief introduction to the use of redis's List type combined with lpush and brpop

First of all, the List of redis is equivalent to a queue, which can implement the first-in-first-out rule.

Brpop is adopted because it blocks when it is not in the queue until there is a popup element in the queue or the wait timeout

Simulation question:

Too many visits, the server processing speed is too slow, if every other user waits, the server feedback, the time is too long, http connection timed out, a server error occurs.

Simulate the implementation process:

There is a client that constantly puts things (data) into the queue, using multithreading to simulate the access of a large number of users.

There is a server that constantly takes prints out of the queue and sets the time for each print to sleep for 2 seconds.

The List structure of redis introduces key [value, value] key represents the name of List, [value,...] It's value.

Customer client.py

Import randomimport threadingimport redisimport configlock = threading.Lock () lock.acquire () lock.release () pool= redis.ConnectionPool (host=config.HOST, port=config.PORT, decode_responses=True, password=config.PASSWORD) r = redis.Redis (connection_pool=pool) # customers put data in redis def fun1 (redisObj): value = random.randint (0100) # store print ("start sending data:", value) redisObj.lpush ("print") in ccc list Str (value)) for i in range: threading.Thread (target=fun1, args= (r,)). Start ()

Server server.py

Import redisimport timeimport configpool = redis.ConnectionPool (host=config.HOST, port=config.PORT, decode_responses=True, password=config.PASSWORD) r = redis.Redis (connection_pool=pool) # the server keeps fetching while True: value = r.brpop ("print") time.sleep (2) print (value). Let's solve the problem of blocking the connection for too long.

How to: take the connection as a function, catch errors, and reconnect when problems occur.

Import redisimport timeimport configdef get_redis (): pool = redis.ConnectionPool (host=config.HOST, port=config.PORT, decode_responses=True) Password=config.PASSWORD) r = redis.Redis (connection_pool=pool) return r # server keeps fetching r = get_redis () while True: try: value = r.brpop ("print") time.sleep (2) print (value) except Exception as e: print ("waiting for timeout reconnection") r = get_redis () Thank you for reading The above is the content of "how to use redis+python to do message queue". After the study of this article, I believe you have a deeper understanding of how to use redis+python to do message queue, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Queue service server problem message continuous customer data learning content situation time time user error blocking thing middle representative element Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei Shulou Technology macOS Apple