0
遅い関数(コードコメントに見られるように)は、今のほんのわずかな要求のために、計11秒で合計11秒です。呼び出しAPIが持つ10秒の制限時間よりも大きなオーダー。低速関数を呼び出すときに、高速な戻り値を取得するにはどうすればよいですか?
これらのAPIの一部はサードパーティ製であるため、最適化はできません。私が必要と考えるのは、APIコールを(通常のシーケンシャルプログラミングの代わりに)非同期タスク、プロセス、またはスレッドにオフロードする方法を得ることです。
@app.route('/webhook', methods=['POST'])
def webhook():
# Get JSON request
jsonRequest = request.get_json(silent=True, force=True)
# Call slow function and get the result
appResult = process_request(jsonRequest)
appResult = json.dumps(appResult, indent=4)
# Make a JSON response
jsonResponse = make_response(appResult)
jsonResponse.headers['Content-Type'] = 'application/json'
return jsonResponse
def process_request(req):
# Call a separate function here or do it all in this one (API Calls, processing etc)
# Return a value
return {
"version": "1.0",
"response": {
"shouldEndSession": True,
"outputSpeech": {
"type": "PlainText",
"text": "Return String"
},
"card": {
"type": "Simple",
"title": "Title",
"content": "Return String"
}
}
}
多分 'celery'を試してみてください? –
これを非同期に実行したい場合は、[asyncio](https://docs.python.org/3/library/asyncio.html)を公式に試してください。個人的に私はそれを使用しようとしましたが、それは私のニーズをサポートしていないと分かって、代わりにマルチスレッドをやり終えましたが、かなり強力なようでした。 – zawata
11は10より大きいオーダーではありません。それはタイプミスですか、1秒オフにしたいですか? – roganjosh