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")
相关推荐
学困昇8 分钟前
C++11中的右值引用和移动语义
开发语言·c++
有梦想的攻城狮9 分钟前
初识Rust语言
java·开发语言·rust
鱼骨不是鱼翅9 分钟前
力扣hot100----1day
python·算法·leetcode·职场和发展
2501_9412362111 分钟前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python
程序猿_极客12 分钟前
【2025 最新】 Python 安装教程 以及 Pycharm 安装教程(超详细图文指南,附常见问题解决)
开发语言·python·pycharm·python安装以及配置
2501_9412357314 分钟前
C++中的装饰器模式变体
开发语言·c++·算法
2501_9411112514 分钟前
基于C++的爬虫框架
开发语言·c++·算法
b***666116 分钟前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
ModestCoder_22 分钟前
Tokenization的演进:从NLP基石到多模态AI的“通用翻译器”
开发语言·人工智能·自然语言处理·机器人·具身智能
chushiyunen25 分钟前
django使用笔记
笔记·python·django