Gunicorn
WSGIの実装。 ASGI で Uvicornと一緒に動かすのも。
公式: Gunicorn - WSGI server — Gunicorn documentation
@202104で version 20.1.0
大枠:
Gunicornのアーキテクチャ - 清水川のScrapbox
モニタープロセスとワーカープロセス
install
pythonの使う環境からinstallするべき(今なら、Poetry, pipenvなど)
$ python -m pip install gunicorn
ubuntuからいれると、古いし、python2対応とかになる。
$ apt show gunicorn
config :
Configuration Overview — Gunicorn 19.9.0 documentation
pythonなので、pythonの書式で configファイルを書く。
worker_class
以下は、Uvicorn使うので、もう要らない?
gunicornのSync/Asyncワーカーの挙動を調べる - Qiita
よく読んでないけど、asyncでパフォーマンス上がるなら、調べて、使いたい。
Gunicorn Workers and Threads - Stack Overflow
workerの設定はあるけど、threadは? answerで、worker, threadも適度にみたいな話があるけど、threadの設定項目は、
Settings — Gunicorn 19.9.0 documentation
defaultは1のようだ。
reload:
開発時には使わない想定だけど、切り替えの狭間?で、gunicornにもreload欲しいときがある。
command line上では、--reload でok
Settings — Gunicorn 20.1.0 documentation
ただ、configファイル上では効かない。documentも configへの言及がない。
inotifyをdefaultで使うようだが、、
error例
flaskに loggingで、
code:python
logging.basicConfig(filename='logging.log', level=logging.DEBUG,
format="%(levelname)s:%(module)s:%(thread)d: %(message)s")
としたら、gunicornが立ち上がらなくなった....理由は不明。
gunicornの error messageは、
gunicorn.service: Main process exited, code=exited, status=3/NOTIMPLEMENTED
これで、検索しても、
Random crashes with status=3/NOTIMPLEMENTED · Issue #1324 · syncthing/syncthing
Deploy
Deploying Gunicorn — Gunicorn 19.9.0 documentation
非同期で使う
gunicornのSync/Asyncワーカーの挙動を調べる - Qiita
-k sync か -k genv か
bottleとgeventによる高速軽量非同期ウェブアプリ – はむかず!
、同時に動くスレッド数は20個程度に抑えられているのが通常である。一方で、ウェブアプリではその内部処理でデータベースアクセスや他サーバへのアクセスを伴うことが多いので
新たなスレッドを立ち上げることなく擬似的なスレッドで他のリクエストを処理しようというのがbottleとgeventを使うときの考え方である
systemdでservice作成
参考
Flask + Gunicorn + Nginx + Supervisor ちゃんと動くまで - Qiita #Supervisor
gunicornでgraceful restart(reload application)する方法 - カカドゥ開発者ブログ
Ask Flask How to deploy multiple apps on a single server using nginx and gunicorn : flask
#deploy