bottle.py 客户端断开连接时停止
bottle.py stalls when client disconnects
我有一个用 bottle 编写的 python 服务器。当我使用 Ajax 从网站访问服务器,然后在服务器发送响应之前关闭网站时,服务器在尝试将响应发送到不再存在的目的地时卡住了。发生这种情况时,服务器会在大约 10 秒内对任何请求无响应,然后再恢复正常操作。
我该如何防止这种情况发生?如果发出请求的网站不再存在,我希望 bottle 立即停止尝试。
我这样启动服务器:
bottle.run(host='localhost', port=port_to_listen_to, quiet=True)
服务器唯一公开的url是这样的:
@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
是一个 long-运行 函数吗? (比如说,10 秒?)
如果是这样,您的唯一服务器线程将与该函数绑定,并且在此期间不会为后续请求提供服务。您是否尝试过 运行 并发服务器,例如 gevent?试试这个:
bottle.run(host='localhost', port=port_to_listen_to, quiet=True, server='gevent')
我有一个用 bottle 编写的 python 服务器。当我使用 Ajax 从网站访问服务器,然后在服务器发送响应之前关闭网站时,服务器在尝试将响应发送到不再存在的目的地时卡住了。发生这种情况时,服务器会在大约 10 秒内对任何请求无响应,然后再恢复正常操作。
我该如何防止这种情况发生?如果发出请求的网站不再存在,我希望 bottle 立即停止尝试。
我这样启动服务器:
bottle.run(host='localhost', port=port_to_listen_to, quiet=True)
服务器唯一公开的url是这样的:
@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
是一个 long-运行 函数吗? (比如说,10 秒?)
如果是这样,您的唯一服务器线程将与该函数绑定,并且在此期间不会为后续请求提供服务。您是否尝试过 运行 并发服务器,例如 gevent?试试这个:
bottle.run(host='localhost', port=port_to_listen_to, quiet=True, server='gevent')