3

Python/flask を使用した REST API バックエンドがあり、イベント ストリームで応答をストリーミングしたいと考えています。すべてが nginx/uwsgi ( https://hub.docker.com/r/tiangolo/uwsgi-nginx-flask/ )を使用して docker コンテナー内で実行されます。

API は、イベント ストリームになるまで正常に動作します。サーバーが計算を終了し、すべてが一緒に送信されるまで、どの種類のクライアントも何も受信しないため、何か(おそらくnginx)が「収量」をバッファリングしているようです。

次のような追加の構成 (nginx_streaming.conf) ファイルを使用して、nginx の設定を (docker イメージの指示に従って) 調整しようとしました。

server {
  location / {
        include uwsgi_params;
        uwsgi_request_buffering off;
  }
}

ドッカーファイル:

FROM tiangolo/uwsgi-nginx-flask:python3.6
COPY ./app /app
COPY ./nginx_streaming.conf /etc/nginx/conf.d/nginx_streaming.conf

しかし、私はnginxの設定にあまり詳しくなく、ここで何をしているのか確信が持てません^^これは少なくとも機能しません..何か提案はありますか?

私のサーバー側の実装:

from flask import Flask
from flask import stream_with_context, request, Response
from werkzeug.contrib.cache import SimpleCache
cache = SimpleCache()
app = Flask(__name__)

from multiprocessing import Pool, Process
@app.route("/my-app")
def myFunc():
    global cache

    arg = request.args.get(<my-arg>)
    cachekey = str(arg)
    print(cachekey)

    result = cache.get(cachekey)
    if result is not None:
        print('Result from cache')
        return result
    else:
        print('object not in Cache...calculate...')
        def calcResult():
            yield 'worker thread started\n'

            with Pool(processes=cores) as parallel_pool:
                [...]

            yield 'Somewhere in the processing'
            temp_result = doSomethingWith(

            savetocache = cache.set(cachekey, temp_result, timeout=60*60*24) #timeout in seconds

            yield 'saved to cache with key:' + cachekey +'\n'
            print(savetocache, flush=True)
            yield temp_result

        return Response(calcResult(), content_type="text/event-stream")

if __name__ == "__main__":
    # Only for debugging while developing
    app.run(host='0.0.0.0', debug=True, port=80)
4

2 に答える 2