当前位置: 首页 > news >正文

python redis中blpop和lpop的区别

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

    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 ofthe list. When provided with the optional ``count`` argument, the replywill 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或者不传则无期限代码阻塞,直到有新的对象加入到集合)。

def blpop(self, keys: List, timeout: Optional[int] = 0) -> Union[Awaitable[list], list]:"""LPOP a value off of the first non-empty listnamed in the ``keys`` list.If none of the lists in ``keys`` has a value to LPOP, then blockfor ``timeout`` seconds, or until a value gets pushed on to oneof the lists.If timeout is 0, then block indefinitely.For more information see https://redis.io/commands/blpop"""

测试lpop()方法:

import redis
import timeif __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()方法:

import redis
import timeif __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)

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

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

import redisif __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")

http://www.lryc.cn/news/320490.html

相关文章:

  • 第四百一十回
  • 程序员的README——编写可维护的代码(一)
  • 数据库管理-第160期 Oracle Vector DB AI-11(20240312)
  • (C++进阶)boost库笔记
  • MapReduce面试重点
  • C语言简单题(7)从主函数中输入10个等长字符串,用一个函数对他们排序,然后在主函数输出这10个已排好序的字符串
  • 光伏科普|太阳能光伏发电应用场景有哪些?
  • Go 构建高效的二叉搜索树联系簿
  • 基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的交通信号灯识别系统(深度学习+UI界面+训练数据集+Python代码)
  • 以太坊开发学习-solidity(三)函数类型
  • 教你把公司吃干抹净、榨干带走
  • 开发指南007-导出Excel
  • 滑块验证码
  • cmd常用指令
  • 【嵌入式DIY实例】-DIY手势识别和颜色识别(基于APDS9960)
  • python 直方图
  • 如何在数据库中使用sql语言插入数据
  • JVM的双亲委派模型和垃圾回收机制
  • ThreadLocal-内存泄露问题
  • ISIS默认层级实验简述
  • 在Flutter中创建自定义的左对齐TabBar组件
  • 【Python】继承会遇到的问题
  • 相机模型Omnidirectional Camera(全方位摄像机)
  • 论文阅读——Align before Fuse
  • 鸿蒙Harmony应用开发—ArkTS声明式开发(基础手势:Rating)
  • Unity中的网格创建和曲线变形
  • day0 3r文档docker部署
  • PSCA复位控制集成之复位信号
  • C#,数值计算,数据测试用的对称正定矩阵(Symmetric Positive Definite Matrix)的随机生成算法与源代码
  • EventWaitHandle 和 lock使用区别