python - shelve 模块偶尔出错(pickle.UnpicklingError: pickle data was truncated and EOFError: Ran out of input)
问题描述
我正在编写一个侦听流并通知其订阅者有关帖子的机器人。我正在使用模块shelve
,它给了我对我来说似乎是随机错误(它们通常不会发生,但有时在启动机器人时它们会发生并且机器人不再能够启动,直到我删除我的数据库文件) . 这意味着由于数据丢失,一些已经发送给订阅者的帖子将被重新发送。
出现了两个错误:
EOFError: Ran out of input
和
pickle.UnpicklingError: pickle data was truncated
我(我相信)能够诊断出在 IO 操作期间机器人被中断(例如 KeyboardInterrupt)时发生的 EOFError 的原因。这个错误不是一个问题,因为它在现实世界的使用中不会经常发生,但当它发生时,我仍然被迫删除整个数据库。
“泡菜数据被截断”错误对我来说仍然是一个谜,因为我似乎无法弄清楚它何时发生或究竟出了什么问题。它也经常发生。引发错误的行是:if message.id in self.db['processed_messages']:
。这是除了对数据库执行任何操作的构造函数之外的第一行。
我希望在解决这两个错误方面有所帮助。我包含了与错误相关的代码,以防有人看到可能导致错误的原因。
与我的问题相关的代码:
import shelve
class Bot():
def __init__(self):
# get data from file or create it if it does not exist
self.db = shelve.open('database')
# init keys if they do not exist (example: first time running the program with empty database):
if 'processed_messages' not in self.db:
self.db['processed_messages'] = []
if 'processed_submissions' not in self.db:
self.db['processed_submissions'] = []
if 'subscribers' not in self.db:
self.db['subscribers'] = []
def handler(self, message):
if message.id in self.db['processed_messages']:
return
self.store_data('processed_messages', message.id)
def store_data(self, key, obj):
""" stores data locally with shelve module """
temp = self.db[key]
temp.append(obj)
self.db[key] = temp
完全不同的说明:如果有人知道处理空数据库情况的更好(更优雅)的方法,我也很想听到一些输入,因为构造函数中的当前方法相当不稳定
解决方案
推荐阅读
- r - 用 R 填充长格式的不完整相关矩阵
- postgresql - Docker-compose:应用程序容器无法连接到 Postgres
- javascript - 如何使用猫鼬在Nodejs中对填充结果的子文档数组进行排序
- php - Zend SOAP 服务器日志功能
- javascript - 如何记录变量/数组/属性包含构造函数?
- angular - 如何使用 Webpack 或 Angular CLI 将 Angular 元素编译为 Web 组件?
- scala - Scala DI 使用带有自我类型注释的特征与抽象类?
- python - 在 yattag 中复制 xml.etree 示例
- python - 从 .txt 数字文件中查找 Mutuals
- php - 在正文 phpMailer 上发送图像的正确方法