2013-08-16 16 views
2

我所知道的唯一方法(從搜索和檢查GEVENT的源)到正常關閉GEVENT WSGI基於服務器是:GEVENT(PY)WSGI正常關機

server = gevent.wsgi.WSGIServer(('', 80), someWSGIApp) 
def shutdown(): 
    print('Shutting down ...') 
    server.stop(timeout=60) 
    exit(signal.SIGTERM) 
gevent.signal(signal.SIGTERM, shutdown) 
server.serve_forever() 

現在,我的意思是優美等待所有greenlet自行終止。例如,如果他們仍在提供請求,他們可以正確完成。

問題是,與上述看似正確的代碼,服務器確實等待最大。爲60秒,但所有TCP連接在收到SIGTERM後立即終止。然而,Greenlets繼續做它們(例如睡覺),直到它們完成或發生超時。

任何想法?

+0

你有沒有找到辦法做到這一點? –

回答

1

由於文檔字符串中服務器的停止方法說(gevent.baseserver.BaseServer:停止)

Stop accepting the connections and close the listening socket. 

If the server uses a pool to spawn the requests, then :meth:`stop` also 
for all the handlers to exit. 
If there are still handlers executing after *has expired (default 1 second), 
then the currently running handlers in the pool are killed. 

我沒有嘗試這一點,但如果文檔是正確的,你應該能夠像這樣以獲得正常停止:

from gevent.pool import Pool 

pool_size = 8 
worker_pool = Pool(pool_size) 
gevent.wsgi.WSGIServer(('', 80), someWSGIApp, spawn=worker_pool)