bottle.py在客户端断开时挂起

问题描述:

我有一个用瓶子写的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)) 

process_message_from_scenario一个长期运行的功能? (说,10秒?)

如果是这样,你的一个和唯一的服务器线程将被捆绑在该功能,并没有后续请求将在此期间服务。您是否尝试过运行并发服务器,如gevent?试试这个:

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

使用gevent修复它。 –