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")
相关推荐
weixin_4404016917 小时前
Python数据分析(Series+DataFrame+部分运算+groupby)
开发语言·python·数据分析
宇木灵17 小时前
C语言基础-八、结构体和共同(用)体
c语言·开发语言·数据结构·笔记·学习·算法
拳里剑气17 小时前
C++ 11
开发语言·c++·学习方法
孞㐑¥17 小时前
算法—穷举,爆搜,深搜,回溯,剪枝
开发语言·c++·经验分享·笔记·算法
2401_8288906417 小时前
通用唤醒词识别模型 - Wav2Vec2
人工智能·python·深度学习·audiolm
nita张17 小时前
2026年2月实战案例分享:合肥战略定位成果解析
人工智能·python
桂花很香,旭很美17 小时前
大模型项目实战:Python 异步——提速、流式、多 Agent
python·language model
yaoxin52112317 小时前
330. Java Stream API - 处理 Optional 对象:像流一样优雅地使用 Optional
java·windows·python
aningxiaoxixi17 小时前
Android Audio 广播之 ACTION_AUDIO_BECOMING_NOISY
android·java·python
Highcharts.js17 小时前
如何根据派生数据创建钟形曲线图表?highcharts正态分布曲线使用指南:从创建到设置一文搞定
开发语言·javascript·开发文档·正态分布·highcharts·图表类型·钟形图