首页 > 数据库 >redis+python练习小问题

redis+python练习小问题

时间:2024-02-04 17:13:25浏览次数:26  
标签:python redis 练习 Redis queue 队列 import message

 

1、“cannot import name 'Redis' from 'redis' "

//python文件名用了“redis.py”,改成其他的就好了。这个一定要注意,很容易犯这种错,想要做什么功能,就用这个功能命名。

2、NameError:name 'redis' is not defined

//我开始是from redis import Redis,改成import redis,就可以了

3、set第一次后,隔了两天继续set,就报错“【Redis】报错:Redis is configured to save RDB snapshots, but it is currently not able to persist on disk”

//是因为强制把redis快照关闭了,导致不能持久化的问题,在网上查了一些相关解决方案,通过stop-writes-on-bgsave-error值设置为no即可避免这种问题。命令行下如下操作:

127.0.0.1:6379> config set stop-writes-on-bgsave-error no

 

【练习】请将 Redis 作为 Python 的消息队列,实现消息的顺序存储和顺序读取。Python 进程结束再重新启动,保证消息队列中的数据不会丢失。

 

import redis
import atexit

# 连接到本地Redis服务器,默认端口是6379
redis_client = redis.Redis(host='localhost', port=6379, decode_responses=True)

# 定义消息队列的键名
message_queue_key = 'message_queue'

# 在程序退出时保存队列数据到Redis
def save_queue_to_redis():
redis_client.ltrim(message_queue_key, 0, -1)

# 在程序退出时注册保存队列数据到Redis的函数
atexit.register(save_queue_to_redis)

# 生产者向队列中添加消息
def enqueue_message(message):
redis_client.rpush(message_queue_key, message)

# 消费者从队列中获取消息
def dequeue_message():
return redis_client.lpop(message_queue_key)

# 添加两条消息
enqueue_message("Message 1")
enqueue_message("Message 2")

# 在重新启动后,从队列中读取消息
message = dequeue_message()
while message:
print("Received Message:", message)
message = dequeue_message()

标签:python,redis,练习,Redis,queue,队列,import,message
From: https://www.cnblogs.com/1234roro/p/18006541

相关文章

  • python 决策曲线 DCA
    importnumpyasnpimportmatplotlib.pyplotaspltfromsklearn.metricsimportconfusion_matrixdefcalculate_net_benefit_model(thresh_group,y_pred_score,y_label):net_benefit_model=np.array([])forthreshinthresh_group:y_pred_lab......
  • Python实现给视频添加字幕
    主要思路:1.用moviepy库处理视频文件;用pysrt库处理字幕。2.由于moviepy依赖名为ImageMagick免费开源图片编辑软件,所以要先安装ImageMagick开始:1.安装ImageMagick到官网 https://www.imagemagick.org/script/download.php#windows下载我这里选择ImageMagick-7.1.1-27-Q16-......
  • redis 常识
    什么是Redis?Redis(RemoteDictionaryServer)Redis是一个开源的使用ANSIC语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API的非关系型数据库。传统数据库遵循ACID规则。而Nosql(NotOnlySQL的缩写,是对不同于......
  • 11 - 初步了解Python
    初步了解Python参考资料:菜鸟教程:Python3基础语法PEP8:StyleGuideforPythonCodePythonDocs:SourceCodeEncoding菜鸟教程:Python3命令行参数PythonDocs:ExecutablePythonScripts知乎:#!/usr/bin/envpython有什么用?编程规范:PEP8在没有额外编程规范的前提下,建议翻阅并......
  • Yield Keyword, classmethod and static method, and Property Method in Python
    ReferenceWhatisYieldKeywordinPythonPython'syieldkeywordislikeanotheroneweusetoreturnanexpressionorobject,typicallyinfunctions,calledreturn.Thereisasmallamountoffluctuation,though.Theyieldstatementofafunctionre......
  • 基于binlog+Canal+Redis 数据一致性
    基于binlog+Canal+Redis方案是一种解决分布式缓存和数据库之间数据一致性问题的方法,它通过MySQL的binlog和Canal机制,实现数据同步到Redis缓存,以保证数据一致性。   . MySQL主备复制原理 2.MySQL中binlog配置 3.Canal工作原理、安装、配置、使用 4.SpringBoot......
  • python3 模型日记
    说明作为一种python框架模型的记录吧,用于个人总结,不定时更新。正文1.主进程退出后,子进程也跟着退出之前遇到过一种情况,用flet写了一个页面,然后又同时开了一个tcpserver的子线程,flet页面点击关闭后,tcpserver却没有退出。在linux中按Ctrl+c可以强制结束,但是如......
  • redis缓存
    放入缓存中的数据应该指定过期时间,为什么呢, 因为过期时间可以确保缓存中的数据不会永久存在,从而避免缓存中出现过时或无效的数据。指定过期时间有以下几个重要原因:1. 数据更新和一致性:当数据发生变化时,缓存中的数据可能会变得过时。如果不过时,新的数据就更新不了,就失去了缓存......
  • Python 矩阵运算
    #coding=utf8fromrequests.sessionsimportsessionimportpubimportnumpyasnpimportdatetimeimportosfromapscheduler.schedulers.blockingimportBlockingSchedulerdefget_default_conn():  conn=(host="127.0.0.1",  port="3306&......
  • Redis大key问题
    什么是大key很多朋友肯定在想redis的key能有多大呀?这里就有个误区了,所谓的大key问题是某个key的value比较大,所以本质上是大value问题。“这样就对上了,key往往是程序可以自行设置的,value往往不受程序控制,因此可能导致value很大。设想一种场景:“在线音乐app中,某个歌单有很多......