【发布时间】:2016-05-01 23:26:22
【问题描述】:
我有一个 python 服务器,它目前正在跟踪我大学中所有公共汽车的位置,并生成到达特定位置的预测。
现在,我想将一个轻量级 REST API 附加到此服务器,但我一直在运行 intro 问题。
我尝试使用带有以下代码的烧瓶:
from flask import Flask, jsonify
from PredictionWrapper import *
import threading
class RequestHandler():
def __init__(self,predictionWrapper):
self.app = Flask(__name__)
self.predictor = predictionWrapper
self.app.debug = False
self.app.add_url_rule('/<route>/<int:busStop>','getSinglePrediction',self.getSinglePrediction)
t = threading.Thread(target=self.app.run, kwargs={'host':'0.0.0.0', 'port':80, 'threaded':True})
t.start()
def getSinglePrediction(self, route, busStop):
# TODO Get the actual prediction with given parameters
prediction = self.predictor.getPredictionForStop(route, busStop)
return jsonify({'busStop': busStop, 'prediction': prediction})
def getStopPrediction(self, busStop):
# TODO Get the actual prediction with given parameters
return jsonify({'busStop': busStop, 'prediction': 2})
def run(self):
self.app.run(host='0.0.0.0', port=80, threaded=True)
问题是我在运行服务器大约半天后遇到了以下错误。请注意,在服务器失败并出现以下错误时,没有向服务器发出任何请求:
ERROR:werkzeug: - - [01/May/2016 09:55:55] 代码 400,消息错误请求语法 ('\x02\xfd\xb1\xc5!')
经过调查,我认为我需要部署到 WSGI 生产服务器。但是我不知道在这种特定方法中它意味着什么,因为 1)烧瓶服务器正在线程化以运行预测应用程序的其余部分,以及 2)我正在使用没有文档使用的类。
任何有关如何使用 apache、gunicorn 或您选择的技术设置 wsgi 文件的帮助将不胜感激。此外,任何关于制作非阻塞 REST API 的更好方法的 cmet 都会有所帮助。
如果您需要任何进一步的说明,请告诉我!
【问题讨论】:
标签: python rest asynchronous flask