程序重启后 Pickle 文件是干净的

Pickle file is clean after program restart

我使用 Heroku。当我用 pickle 保存文件时,一切正常,但程序重新启动后文件是干净的。我用这两种方法

@staticmethod
def save_obj(obj, name):
    with open(f'plugins/utils/obj/{name}.pkl', 'wb') as f:
        pickle.dump(obj, f)

@staticmethod
def load_obj(name):
    with open(f'plugins/utils/obj/{name}.pkl', 'rb') as f:
        return pickle.load(f)

通过以下方式

if cmd[1] == 'b':  # add in dict
    bought = Utils.load_obj('bought')
    print(bought)
    bought[msg.user_id] = {'RU': {}}
    print(bought)
    Utils.save_obj(bought, 'bought')
elif cmd[1] == 'i':  # create empty dict
    Utils.save_obj({}, 'bought')
elif cmd[1] == 's':  # print all from dict
    print(Utils.load_obj('bought'))

Heroku's filesytem is ephemeral. Any changes you make to it are lost when your dyno restarts, which happens frequently(每天至少一次)。此外,您可以在此处手动重启测功机。

Heroku recommends storing uploads on something like Amazon S3, but depending what you're saving a client-server database might be a better solution. Heroku supports many and should provide you with a PostgreSQL database out of the box. You can access it via the DATABASE_URL environment variable.