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")
相关推荐
重生之我在20年代敲代码33 分钟前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
爱上语文34 分钟前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
小安运维日记2 小时前
Linux云计算 |【第四阶段】NOSQL-DAY1
linux·运维·redis·sql·云计算·nosql
waterHBO2 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七3 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286114 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py4 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy4 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
AIAdvocate5 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼5 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt