首页 > 解决方案 > 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

完全不同的说明:如果有人知道处理空数据库情况的更好(更优雅)的方法,我也很想听到一些输入,因为构造函数中的当前方法相当不稳定

标签: pythonpickleshelve

解决方案


推荐阅读