【问题标题】:R10/H10 application error when trying to open Flask app with Heroku [duplicate]尝试使用 Heroku 打开 Flask 应用程序时出现 R10/H10 应用程序错误 [重复]
【发布时间】:2020-06-27 11:38:34
【问题描述】:

我和一个朋友编写了一个 Python 应用程序,用于在世界地图上按国家/地区绘制 Covid-19 病例数。

这是我们的代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 15 17:01:50 2020

@author: sheldon
"""

import os 
import pandas as pd
import folium
from folium import plugins
import rasterio as rio
from rasterio.warp import calculate_default_transform, reproject, Resampling
import earthpy as et
import pdb 
import flask
from flask import Flask




class CovidDF:
    def __init__(self, url):
        self.url = url
        self.raw = None
        self.aggregated = None

    def reload(self, date):
        self.raw = pd.read_csv(self.url)
        self.group_by_regions(date)

    def group_by_regions(self,date):
          df=self.raw[['Province/State','Country/Region','Lat','Long',date]]
          self.aggregated=df.groupby(['Country/Region']).agg({'Lat':'mean',
                              'Long':'mean',
                              date: 'sum'})
          self.aggregated.at['France','Lat']=46.2276
          self.aggregated.at['France','Long']=2.2137


class CovidData(object):

    def __init__(self):
        self.confirmed_cases = CovidDF('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv')
        self.deaths = CovidDF('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv')
        self.recoveries = CovidDF('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Recovered.csv')
        self.loaded = False
        self.map = folium.Map(location=[0,0],
              tiles = 'Stamen Terrain',
              zoom_start=2)
        
    def populate(self, date):
        if not self.loaded:
             self.confirmed_cases.reload(date)
             self.deaths.reload(date)
             self.recoveries.reload(date)
             self.loaded=True

    def plot_number_of_cases(self,df,date,custom_color):
          dc=df.iloc[df[date].to_numpy().nonzero()]
          latitude = dc.Lat.values.astype('float')
          longitude = dc.Long.values.astype('float')
          radius = dc[date].values.astype('float')
     
          for la,lo,ra in zip(latitude,longitude,radius):
              folium.Circle(
                  location=[la,lo],
                  radius=ra*10,
                  fill=True,
                  color=custom_color,
                  fill_color=custom_color,
                  fill_opacity=0.5
              ).add_to(self.map)

    def plot_number_of_cases_for_all_dataframes(self,date):
          self.plot_number_of_cases(self.confirmed_cases.aggregated,date,'blue')
          self.plot_number_of_cases(self.deaths.aggregated,date,'red')
          self.plot_number_of_cases(self.recoveries.aggregated,date,'green')
          



my_date='3/14/20'
covid_data=CovidData()
covid_data.populate(my_date)
covid_data.plot_number_of_cases_for_all_dataframes(my_date)
#covid_data.map.save("./mytest.html")

app = Flask(__name__)
@app.route("/")
def display_map():
     return covid_data.map._repr_html_()

应用程序在 Heroku 上构建良好,但在尝试打开它时出现应用程序错误。 检查日志会产生以下错误消息:

2020-03-16T10:37:49.600873+00:00 heroku[web.1]: 错误 R10(启动超时)-> Web 进程在启动后 60 秒内无法绑定到 $PORT

2020-03-16T10:37:49.600972+00:00 heroku[web.1]:使用 SIGKILL 停止进程

2020-03-16T10:37:49.697252+00:00 heroku[web.1]:进程以状态 137 退出

2020-03-16T10:41:33.514461+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=covid19-viz.herokuapp.com request_id=2e0727f9-d3eb-4966-8252-92a871deaa41 fwd="77.150.72.89" dyno= connect= service= status=503 bytes= protocol=https

我检查了this other post 并了解该错误与端口规范问题有关,但我不知道如何修复它。任何帮助将不胜感激!

【问题讨论】:

标签: python pandas flask heroku folium


【解决方案1】:

解决方案 1:

通过python app.py 运行应用程序时,添加到您的app.py

if __name__ == "__main__":
  app.run(host='0.0.0.0', port=os.environ.get('PORT', 80))

解决方案 2:

通过flask run 运行应用程序时,将Procfile 调整为:

web: flask run --host=0.0.0.0 --port=$PORT

解决方案 3:

使用 Gunicorn 将 Procfile 调整为:

web: gunicorn app:app

gunicorn 添加到requirements.txt。 Gunicorn 自动绑定到$PORT


在 Heroku 上托管时,您需要绑定到 $PORT,这是 Heroku 作为环境变量提供给您的。

【讨论】:

  • 非常感谢您的帮助,Tin!你的修复成功了!
猜你喜欢
  • 2013-09-11
  • 2016-12-11
  • 2014-03-26
  • 2020-10-21
  • 1970-01-01
  • 2019-01-29
  • 2021-12-06
  • 2021-11-27
相关资源
最近更新 更多