2017-11-18 148 views
2

我有一個用瓶子寫的python服務器。當我使用Ajax從網站訪問服務器,然後在服務器可以發送其響應之前關閉網站時,服務器會嘗試將響應發送到不再存在的目標。發生這種情況時,在恢復正常操作之前,服務器對約10秒的任何請求無響應。bottle.py在客戶端斷開時掛起

我該如何預防?如果發出請求的網站不再存在,我希望瓶子立即停止嘗試。

我開始喜歡這個服務器:

bottle.run(host='localhost', port=port_to_listen_to, quiet=True) 

和服務器暴露的唯一網址是:

@bottle.route('/', method='POST') 
def main_server_input(): 
    request_data = bottle.request.forms['request_data'] 
    request_data = json.loads(request_data) 
    try: 
     response_data = process_message_from_scenario(request_data) 
    except: 
     error_message = utilities.get_error_message_details() 
     error_message = "Exception during processing of command:\n%s" % (error_message,) 
     print(error_message) 
     response_data = { 
      'success' : False, 
      'error_message' : error_message, 
     } 
    return(json.dumps(response_data)) 

回答

2

process_message_from_scenario一個長期運行的功能? (說,10秒?)

如果是這樣,你的一個和唯一的服務器線程將被捆綁在該功能,並沒有後續請求將在此期間服務。您是否嘗試過運行併發服務器,如gevent?試試這個:

bottle.run(host='localhost', port=port_to_listen_to, quiet=True, server='gevent') 
+0

使用gevent修復它。 –