我有一个python服务器,它正在跟踪我大学里所有公交车的位置,并生成到达特定地点的预测。在
现在,我想将一个轻量级restapi附加到这个服务器上,但是我一直在运行介绍问题。在
我试着用下面的代码使用烧瓶:
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)
问题是我在运行服务器大约半天之后遇到了下面的错误。请注意,在服务器发生故障时,没有向服务器发出任何请求,错误如下:
在错误:werkzeug:-[01/May/2016 09:55:55]代码400,消息错误请求语法('\x02\xfd\xb1\xc5!')在
经过调查,我认为我需要部署到一个WSGI生产服务器。但我不知道这种特定方法意味着什么,因为1)flask服务器正在被线程化,以便运行其余的预测应用程序;2)我使用的类没有任何文档使用。在
如果有任何关于如何使用apache、gunicorn或您选择的技术来设置wsgi文件的帮助,我们将不胜感激。此外,关于如何更好地创建非阻塞restapi的任何评论都会很有帮助。在
如果你需要进一步的说明,请告诉我!在
要做的第一件事是将异常处理放在处理错误的JSON请求数据(这可能就是正在发生的事情)类似于
不确定这是否真的能解决您的问题,但您可以使用基于协同程序的web服务器gevent。他们有一个WSGI服务器,如果你说你需要部署一个WSGI生产服务器,你可以使用它。在
如果要在flask应用程序中实现服务器,请执行以下操作:
一般来说,Gevent是一个非常强大的工具,通过在必要时发出上下文开关,它可以非常容易地处理多个客户端。此外,gevent完全支持烧瓶。在
相关问题 更多 >
编程相关推荐