2017-11-18 24 views
2

私はビンで書かれたPythonサーバーを持っています。 Ajaxを使用しているWebサイトからサーバーにアクセスし、サーバーが応答を送信する前にWebサイトを閉じると、サーバーは、もはや存在しない宛先に応答を送信しようとしています。この場合、通常の操作を再開する前に、サーバーは約10秒間何らかの要求に対して応答しなくなります。クライアントが切断されたときにbottle.pyが停止する

どうすればこの問題を防ぐことができますか?リクエストを行ったウェブサイトが存在しなくなった場合は、ボトルをすぐに試してみることをお勧めします。

私はこのようなサーバーを起動します。

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)) 

答えて

2

は、長時間実行される機能process_message_from_scenarioですか? (と言うと、10秒?)

もしそうなら、あなたの1つのみのサーバースレッドはその機能に縛られ、その間に後続の要求は処理されません。 geventのような同時実行サーバーを試してみましたか?これを試してみてください:

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

gevent fixedを使用してください。 –

関連する問題