Home > Database > Redis > How to operate redis message queue in python

How to operate redis message queue in python

WBOY
Release: 2023-05-28 12:55:31
forward
1808 people have browsed it

redis is a Key-Value database. Value supports string (string), list (list), set (collection), zset (ordered set), hash (hash type) and other types. .

Redis Service

  1、安装

  yum install redis

  2、 python安装支持模块

  /opt/python2.7.13/bin/pip install redis

  3、 和redis的简单直接交互

  In [1]: import redis

  In [2]: rc = redis.Redis(host='192.168.8.237',port=6379,decode_responses=True)

  In [5]: rc.set('imoocc','jeson')

  Out[5]: True

  In [7]: rc.get('imoocc')

  Out[7]: u'jeson'
Copy after login

Redis Simple Operation

We can use the connection pool method provided in the Redis class to manage and operate Redis interactions.

  1、连接池建立,利用连接池连接

  In [8]: connpool = redis.ConnectionPool(host='192.168.8.237',port=6379,decode_responses=True)

  In [9]: rc = redis.Redis(connection_pool=connpool)

  In [10]: rc.set('imooccp','1234566')

  Out[10]: True

  In [11]: rc.get('imooccp')

  Out[11]: u'1234566'  2、redis中DB ,指定数据存取的DB

  redis.conf中设置了db的数量,那么redis的数据库名也为0 到15,如下:

  databases 16

  指定使用那个数据库名,我们通过如下的方式,就可以了:

  In [29]: connpool = redis.ConnectionPool(host='192.168.8.237',port=6379,decode_responses=True,db=3)

  In [30]: rc.set('test2','lllll')

  Out[30]: True

  这样就将数据库写入到db3了,如何验证呢?我们从服务端来认证最直接,如下:

  用redis-cli(redis自带的工具)登录服务端,查看写入的key。

  127.0.0.1:6379> SELECT 3

  OK

  127.0.0.1:6379[3]> KEYS * //查看db 3这个库下所有的key

  1) "imooccc"
Copy after login

Then let’s introduce the detailed project usage of redis. Don’t feel that the introduced content is too diffuse.

Recently I am working on a task system. Task writing is irregular. We need to use a program to consume these tasks regularly. There must be a sequence, and only after the previous task is confirmed to be executed. Will go to the next one.

What to do? Use a queue, right! What queue to use and how to use it?

Let me introduce why redis is used for message processing:

Simply put, Redis supports two consumption models, a publish-subscribe model, and a message will be processed by multiple consumers (simply put, it is like a broadcast message, everyone will receive it). Obviously, this approach is not applicable in my system. Instead, the system needs to adopt a queue mode in order to prioritize tasks in the order in which they arrive. So what type of database structure should be used to create a queue?

First let’s talk about list

list This data type is a list in python and an ordered queue in redis (or data linked list).

The type diagram is as follows: How to operate redis message queue in python

Everyone will understand after looking at this structure. We can use the data interface mode of the list to design elements that can be added to the list from both the left and right ends. Same-direction processing (satisfying the characteristics of the queue first come first out).

  1、从左端插入元素

  In [10]: rc.lpush('tasklist',1,2,3)

  Out[10]: 3L

  打印输出内容,如下:

  In [17]: print(rc.lrange('tasklist',0,2))

  [u'3', u'2', u'1']

  类似的从左测插入的方法还有lpushx(name,value),区别只有当name存在才将value插入到最左边。

  2、从右边插入元素

  In [21]: rc.rpush('tasklist','5')

  Out[21]: 5L

  In [22]: print(rc.lrange('tasklist',0,4))

  [u'4', u'3', u'2', u'1', u'5']

  3、从左边取出元素怎么取呢?

  In [23]: rc.lpop("tasklist")

  Out[23]: u'4'  In [24]: rc.lpop("tasklist")

  Out[24]: u'3'  In [25]: print(rc.lrange('tasklist',0,4))

  [u'2', u'1', u'5']

  我们会看到从左侧已经取出了两个元素了。剩下来[u'2', u'1', u'5']

  4、从右侧呢?

  当然就是rc.rpop()了
Copy after login

The above is the detailed content of How to operate redis message queue in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template