安排Heroku上的python脚本每30分钟发布一次,这不仅有用

我构建了一个简单的python机器人程序,该程序每30分钟发布一次推文。使用python time函数,一切在我的本地机器上都运行良好:

import time
sleep.time(1800)

我成功在30分钟后成功发送了该程序。但是部署到Heroku最初并没有成功(使用免费帐户),我离开了python time函数来调节机器人的时间间隔,但这没有用。问题不是它几乎要保持时间像28分钟,27分钟等,而是怪异地随机(例如5分钟间隔),然后是接下来的30分钟,然后是25分钟。

注意:由于Heroku的睡眠规定,我将本地计算机的设置时间从30分钟更改为29分钟。

我查找了这个问题并试图对其进行调整,并构建了一个clock.py程序来控制Heroku dyno。

from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()

#execute very 30 minutes
@sched.scheduled_job('interval',minutes=29)
def scheduled_job():
    #start logic
    sched.start()

机器人仍然会随机发帖,并且不会遵守规定的29分钟间隔。这是-Heroku日志--tail

的输出
2019-11-25T13:30:40.887261+00:00 heroku[scheduler.9975]: State changed from starting to up
2019-11-25T13:30:41.883677+00:00 heroku[scheduler.9975]: State changed from up to complete
2019-11-25T13:30:41.863844+00:00 heroku[scheduler.9975]: Process exited with status 127
2019-11-25T13:30:41.812019+00:00 app[scheduler.9975]: bash: herokugitbot: command not found
2019-11-25T13:50:36.990415+00:00 heroku[clock.1]: State changed from crashed to starting
2019-11-25T13:50:42.027765+00:00 heroku[clock.1]: Starting process with command `python bot.py`
2019-11-25T13:50:42.611562+00:00 heroku[clock.1]: State changed from starting to up
2019-11-25T13:50:44.607705+00:00 heroku[clock.1]: State changed from up to crashed
2019-11-25T13:50:44.587362+00:00 heroku[clock.1]: Process exited with status 0
2019-11-25T14:01:01.725817+00:00 app[api]: Starting process with command `herokugitbot` by user scheduler@addons.heroku.com
2019-11-25T14:01:07.615698+00:00 heroku[scheduler.9046]: Starting process with command `herokugitbot`
2019-11-25T14:01:08.266222+00:00 heroku[scheduler.9046]: State changed from starting to up
2019-11-25T14:01:09.905828+00:00 heroku[scheduler.9046]: State changed from up to complete
2019-11-25T14:01:09.820645+00:00 app[scheduler.9046]: bash: herokugitbot: command not found
2019-11-25T14:01:09.886053+00:00 heroku[scheduler.9046]: Process exited with status 127
2019-11-25T14:03:16.747799+00:00 heroku[clock.1]: State changed from crashed to starting
2019-11-25T14:03:21.593237+00:00 heroku[clock.1]: Starting process with command `python bot.py`
2019-11-25T14:03:22.185303+00:00 heroku[clock.1]: State changed from starting to up
2019-11-25T14:03:24.861963+00:00 heroku[clock.1]: State changed from up to crashed
2019-11-25T14:03:24.846089+00:00 heroku[clock.1]: Process exited with status 0

我该如何确保它坚持正确的29分钟时间?

我尝试了Heroku调度程序,但只允许10分钟,1小时零1天

szchengw 回答:安排Heroku上的python脚本每30分钟发布一次,这不仅有用

如果您不介意确切地在13:30、14:00、14:30等发布信息,则可以执行以下操作:

import time
import datetime

while True:
    current_time = datetime.datetime.now()
    if current_time.minute in [0,30]:
        # your function that tweets
        post_tweet()
        # sleep to avoid running the function again in the next loop
        time.sleep(120)

如果您想在第一次启动脚本时发推文,然后在30分钟后再次发推文,则可以使用以下命令:

import time
import datetime

start_time = datetime.datetime.now()
interval = start_time + datetime.timedelta(minutes=30)

# dynamically create the interval times
tweet_times = [start_time.minute,interval.minute]

while True:
    current_time = datetime.datetime.now()
    if current_time.minute in tweet_times:
        # your function that tweets
        post_tweet()
        # sleep to avoid running the function again in the next loop
        time.sleep(120)
本文链接:https://www.f2er.com/3035669.html

大家都在问