tornado.web.Application多线程并发实现

class MainHandler(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(1)

    def initialize(self, relPredict):
        self.predict = relPredict

    def get(self):
        self.write("Hello, world")

    @tornado.gen.coroutine
    def post(self):
        resDict = yield self.getSentimentScore()
        self.write(resDict)

    @run_on_executor
    def getSentimentScore(self):
        param = self.request.body
        param_json = json.loads(param.decode('utf-8'))
        text = param_json['text']
        # 设置模式为行分词模式
        # prediect = LSTMPredict()
        with graph.as_default():
            # print("开始预测……")
            result = self.predict.predict(text)
        resDict = {}
        resDict["y_pred"] = str(result[0])
        # print(resDict)
        return resDict
if __name__ == "__main__":
    predict = LSTMPredict()
    graph = tf.get_default_graph()
    app = tornado.web.Application(handlers=[(r"/sentimentAnalysis", MainHandler, {"relPredict": predict})],
                                  autoreload=False, debug=False)
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.bind(9996)
    http_server.start()
    tornado.ioloop.IOLoop.instance().start()

你可能感兴趣的:(tornado.web.Application多线程并发实现)