python redis中blpop和lpop的区别

python redis中lpop()方法是获取并删除左边第一个对象。

python 复制代码
    def lpop(
        self,
        name: str,
        count: Optional[int] = None,
    ) -> Union[Awaitable[Union[str, List, None]], Union[str, List, None]]:
        """
        Removes and returns the first elements of the list ``name``.

        By default, the command pops a single element from the beginning of
        the list. When provided with the optional ``count`` argument, the reply
        will consist of up to count elements, depending on the list's length.

        For more information see https://redis.io/commands/lpop
        """

python redis中blpop()方法是获取并删除左边第一个对象,并多了一个阻塞的作用(如果传入的timeout参数大于零,执行blpop()是会检查集合里面有没有对象,没有对象则进行阻塞timeout秒,如果timeout为0或者不传则无期限代码阻塞,直到有新的对象加入到集合)。

python 复制代码
    def blpop(
        self, keys: List, timeout: Optional[int] = 0
    ) -> Union[Awaitable[list], list]:
        """
        LPOP a value off of the first non-empty list
        named in the ``keys`` list.

        If none of the lists in ``keys`` has a value to LPOP, then block
        for ``timeout`` seconds, or until a value gets pushed on to one
        of the lists.

        If timeout is 0, then block indefinitely.

        For more information see https://redis.io/commands/blpop
        """

测试lpop()方法:

python 复制代码
import redis
import time



if __name__ == "__main__":
    # 使用连接池来管理多个连接
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379,password="123456", db=0)
    redis_client = redis.StrictRedis(connection_pool=pool)



    while True:
        #获取消息队列
        print("读取mq队列")
        task = redis_client.lpop("task")

        if task is not None:
            print("收到消息: " + str(task))
            #执行任务

        time.sleep(1)

结果每秒会获取一次:

测试blpop()方法:

python 复制代码
import redis
import time

if __name__ == "__main__":
    # 使用连接池来管理多个连接
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379,password="123456", db=0)
    redis_client = redis.StrictRedis(connection_pool=pool)

    while True:
        #获取消息队列
        print("读取mq队列")
        task = redis_client.blpop(_task")

        if task is not None:
            print("收到消息: " + str(task))
            #执行任务

        time.sleep(1)

结果只读取一次,然后就阻塞了:

直到有对象进入集合,才会继续执行代码。

python 复制代码
import redis


if __name__ == "__main__":
    # 使用连接池来管理多个连接
    pool = redis.ConnectionPool(host='127.0.0.1', port=6379,password="123456", db=0)
    redis_client = redis.StrictRedis(connection_pool=pool)

    # 设置键值对
    redis_client.rpush("task","22")
相关推荐
闲猫1 分钟前
go orm GORM
开发语言·后端·golang
李白同学2 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
希忘auto2 小时前
详解Redis在Centos上的安装
redis·centos
黑子哥呢?3 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
失败尽常态5233 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_904447743 小时前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农3 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿3 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing3 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
彳卸风4 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言