【问题标题】:My uptime function isn't able to go beyond 24 hours on Heroku我的正常运行时间功能在 Heroku 上无法超过 24 小时
【发布时间】:2019-02-08 20:04:19
【问题描述】:

我对 Python Discord 机器人有以下正常运行时间功能:

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 小时或更长时间的正常运行时间(即正常运行时间没有报告天数,似乎在某处中途重置,尽管bot 始终在线)。我认为我的函数可能有问题,所以我添加了打印语句进行调试,并带有示例开始时间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】:

    Heroku 每 24 小时重新启动一次测功机,因此您的正常运行时间永远不会超过 24 小时。您必须将数据保存在像 Redis 这样的存储中,这样它才能跨越重启。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      相关资源
      最近更新 更多