2016-03-30 127 views
0

我制作了一个Flask-Python Web应用程序,用于刮擦皇马的装置并将其显示在整洁的倒计时页面中。我正尝试通过Heroku进行托管。我将scraping部分移到了主python脚本中,并通过render_template传递了刮掉的变量。定期重新启动Heroku Python脚本

我的问题是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) 

P.S:我使用Heroku的首次。请原谅,如果听起来很愚蠢。

回答

0

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

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

希望它有帮助!