繁体   English   中英

定期重启 Heroku Python 脚本

[英]Restarting Heroku Python script periodically

我制作了一个Flask-Python Web 应用程序,它抓取了皇马的固定装置并将其显示在一个整洁的倒计时页面中。 我正在尝试通过Heroku托管它。 我将抓取部分移动到主 python 脚本并通过render_template传递抓取的变量。

我的问题是 Python 脚本如何在 Heroku 服务器上运行? 它是在有人打开网页时调用还是只运行一次并为请求提供服务? 如果是这样,有没有办法重新启动服务器或定期重新运行 Python 脚本,以便固定装置的更改反映在网页中。

这是我的 app.py

import requests
import datetime
from bs4 import BeautifulSoup as bs
from lxml import html
url = 'http://www.realmadrid.com/en/football/schedule'
response = requests.get(url)
html = response.content
soup = bs(html)
loc = soup.find('p', {'class': 'm_highlighted_next_game_location'}).contents
loc1 = loc[0]
if "Santiago" in loc1:
    opp = soup.find('div',{'class':'m_highlighted_next_game_team m_highlighted_next_game_second_team'}).strong.contents
else:
    opp = soup.find('div', {'class': 'm_highlighted_next_game_team'}).strong.contents
opp1=opp[0]
time = soup.find('div', {'class': 'm_highlighted_next_game_info_wrapper'}).time.contents
time1 = time[0]
date = soup.find('header', {'class': 'm_highlighted_next_game_header'}).time.contents
date1 = date[0]
times = time1.split(":")
dates = date1.split("/")

hour = times[0]
mintemp = times[1]
minutes = mintemp[:-2]
year = dates[0]
month = dates[1]
day = dates[2]

from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
    return render_template('index.html',hour=hour,minutes=minutes,year=year,month=month,day=day,loc=loc1,opp=opp1)

if __name__ == '__main__':
    app.run(debug=True)

PS:我是第一次使用 Heroku。 如果有些事情听起来很愚蠢,请原谅。

Heroku 被认为是“懒惰的”,如果服务器闲置超过 30 分钟(也是为了节省电量),它就会停止服务器。 但是,如果您的应用发出请求,它将立即唤醒(可能需要几秒钟才能唤醒)。 在您的情况下,每次打开网站并发出请求时,它都会重新运行 python 脚本。

如果您想定期更新装置而不从您的网站发出请求,请查看Heroku Scheduler ,它可以让您定期安排 heroku 任务。 请记住,对于免费版本,您需要让 heroku 服务器每天至少休眠 6 小时。

希望能帮助到你!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM