我的正常运行时间功能在Heroku上无法超过24小时

问题描述 投票:0回答:1

我有一个Python Discord bot的以下正常运行时间函数:

import datetime

start_time = datetime.datetime.utcnow() # Timestamp of when it came online

@client.command(pass_context=True)
async def uptime(ctx: commands.Context):
    now = datetime.datetime.utcnow() # Timestamp of when uptime function is run
    delta = now - start_time
    hours, remainder = divmod(int(delta.total_seconds()), 3600)
    minutes, seconds = divmod(remainder, 60)
    days, hours = divmod(hours, 24)
    if days:
        time_format = "**{d}** days, **{h}** hours, **{m}** minutes, and **{s}** seconds."
    else:
        time_format = "**{h}** hours, **{m}** minutes, and **{s}** seconds."
    uptime_stamp = time_format.format(d=days, h=hours, m=minutes, s=seconds)
    await client.say("{} has been up for {}".format(client.user.name, uptime_stamp))

我将这个机器人部署到了Heroku(免费等级),并且在几天内观察到我从未能够获得24小时或更长时间的正常运行时间(即正常运行时间没有报告天数,即使机器人在线也似乎重置在某个地方始终)。我觉得我的功能可能有问题,所以我添加了print语句来调试,带有一个示例开始时间,like so.

我的输出显示该函数能够处理差异> 1天:

The start time is: 2018-09-02 00:00:00
The time now is: 2018-09-03 18:58:03.458852
The delta is 1 day, 18:58:03.458852
The time difference in seconds is: 154683
Hours: 42, remainder: 3483 seconds
Minutes: 58, remainder: 3 seconds
Days: 1, remainder: 18 hours
The time difference is over one day.
**1** days, **18** hours, **58** minutes, and **3** seconds
>>> 

两个问题:

1)我的正常运行功能是否合理? 2)我现在正在读Heroku可能每24小时重置一次他们的免费动态 - 如果是这样的话,我怎样才能获得实际工作几天的正常运行功能?

谢谢!

python python-3.x heroku discord.py
1个回答
1
投票

Heroku每24小时重新启动你的d​​yno,所以你永远不会看到超过24小时的正常运行时间。您必须在Redis等商店中保留数据,以便重新启动。

© www.soinside.com 2019 - 2024. All rights reserved.