2016-12-27 62 views
1

當我做一個web應用程序使用torado + flask,我遇到了一個問題,當我向我的應用程序發送請求時,它對我沒有任何迴應,它總是等待。 當我發現問題時,我發現在我的服務器機器(linux)中有很多'close wait'。 我不知道如何解決這個問題,任何人都可以幫助我嗎? 這裏是我使用龍捲風代碼:龍捲風燒瓶關閉等待

#coding=utf-8 
from tornado.wsgi import WSGIContainer 
from tornado.httpserver import HTTPServer`enter code here` 
from tornado.ioloop import IOLoop 
from service import app #app is a flask in another file:app=Flask(__name__) 
from config import SERVER_CONF 
from appLog import logging 


def startService(): 
    logging.info('start web,http://%s:%s/test'%(SERVER_CONF['IP'],SERVER_CONF['PORT'])) 
    try: 
     http_server=HTTPServer(WSGIContainer(app)) 
     http_server.listen(SERVER_CONF['PORT'],address=SERVER_CONF['IP']) 
     IOLoop.instance().start() 
    except Exception as e: 
     logging.error('start failed:') 
     logging.error(e) 


if __name__=='__main__': 
    startService() 

回答

0

我的理解是,你需要使用FallbackHandler,如this answer描述。

這就是說,我強烈建議不要使用這種方法 - 龍捲風包括一個優秀的microframework of its own,它與服務器集成得更好,在很多方面都優於Flask。如果使用Flask非常重要,我建議您探索一種確保併發性的不同方法(例如,nginx負載均衡器背後的多個實例),或者看看Sanic,它既是異步的,又非常類似於Flask。

+1

除非您想將* wsgi/flask應用程序與原生龍捲風應用程序*組合起來,否則'FallbackHandler'是不必要的。但你說得對,在Tornado WSGIContainer中運行燒瓶通常是一個壞主意:http://www.tornadoweb.org/en/stable/wsgi.html#tornado.wsgi.WSGIContainer。如果你使用燒瓶,你應該使用gunicorn或uwsgi而不是龍捲風的http服務器。 –

+0

絕對 - 在我的回答中,我只是假設你應該將Flask包裝在WSGI容器中進行生產。 –

+0

謝謝你所有的答案。我會考慮使用gunicorn而不是龍捲風的http服務器。謝謝! – elina