为什么我的Flask会在每次刷新时计算出不同的数字?

时间:2018-01-04 09:13:25

标签: python html flask fastcgi

我使用Flask创建了一个相当简单的Web应用程序。它使用Skyfield计算所有行星的当前距离。

当我在终端本地运行它们时,服务器和Python代码工作正常。不幸的是,当我使用FastCGI将它们部署到我的服务器时,每当我点击刷新时,该网站就会吐出完全不同的数字。我真的无法找出问题所在 - 任何人都可以帮助我吗?

当前网站:Click

主要的Flask代码:

#!/usr/bin/env python3.4 

from flask import Flask, render_template
import calculations

app = Flask(__name__)

@app.route("/")
def index():
    return render_template("main.html", DistList=calculations.DistanceList(), DistRelList=calculations.DistanceRelationList())

@app.route("/sources")
def sources():
return render_template("sources.html")

if __name__=="__main__":
    app.run()

用于计算距离的python:

import numpy
import time
from skyfield.api import load

ts = load.timescale()
planets = load('de421.bsp')

# create dictionary with all data needed. (body, min distance, max distance)
# Planetary Information taken from Nasa Fact Sheets 
# (https://nssdc.gsfc.nasa.gov/planetary/factsheet/)
celestialsDict = {
"Mercury": [planets["mercury"], 77342099, 221853642],
"Venus": [planets["venus"], 38297055, 260898687],
"Mars": [planets["mars"], 55650408, 401371087],
"Jupiter": [planets["JUPITER_BARYCENTER"], 588518023, 968047821],
"Saturn": [planets["SATURN_BARYCENTER"], 1195436585, 1658441995],
"Uranus": [planets["URANUS_BARYCENTER"], 2581909650, 3157263061],
"Neptune": [planets["NEPTUNE_BARYCENTER"], 4305875512, 4687350083],
"Pluto": [planets["PLUTO_BARYCENTER"], 4293758085, 7533299975],
"Moon": [planets["Moon"], 362102, 404694]
}

def DistanceToEarth(body):
    #return current distance to earth
    t = ts.now()
    astrometric = planets["earth"].at(t).observe(body)
    ra, dec, distance = astrometric.radec()
    return float("{:.2f}".format(distance.km))

def DistanceList():
    #returns a list of distances of the celestials
    DistList = []
    for body in celestialsDict:
        DistList.append(DistanceToEarth(celestialsDict[body][0]))
    return DistList

def DistanceRelation(body):
#returns the relative position in percent (0% = closes to earth, 100% = farthest from earth)
  relativePosition = ((DistanceToEarth(celestialsDict[body][0]) - celestialsDict[body][1]) / (celestialsDict[body][2] - celestialsDict[body][1])) * 100
  return relativePosition

def DistanceRelationList():
    DistRelList = []
    for body in celestialsDict:
        DistRelList.append(float("{:.2f}".format(DistanceRelation(body))))
    return DistRelList

这是启动服务器的FCGI脚本:

#!/usr/bin/env python3.4
RELATIVE_WEB_URL_PATH = '/Howfarismars/02_Layout/Code'
LOCAL_APPLICATION_PATH = os.path.expanduser('~') + '/html/Howfarismars/02_Layout/Code'

import sys
sys.path.insert(0, LOCAL_APPLICATION_PATH)

from flup.server.fcgi import WSGIServer
from app import app

class ScriptNamePatch(object):
  def __init__(self, app):
    self.app = app

  def __call__(self, environ, start_response):
    environ['SCRIPT_NAME'] = RELATIVE_WEB_URL_PATH
    return self.app(environ, start_response)

app = ScriptNamePatch(app)

if __name__ == '__main__':
  WSGIServer(app).run()

0 个答案:

没有答案