【python】gunicornとfalconを使ってWSGIサーバを作成してみる
環境
CentOS6
python3.5.1
gunicorn19.6.0
falcon-1.0.0
はじめに
gunicornはPython製のWSGIサーバ。
WSGIサーバーとはWEBサーバーとWebアプリケーションをつなぐサーバ。
今回はwebアプリケーションにpythonのWEBフレームワークfalconを利用する。
前回の記事で導入したfalconアプリケーションを今回は利用する
toriaezu-engineer.hatenablog.com
手順
gunicornインストール
pip install gunicorn
インストール確認。
gunicorn -v gunicorn (version 19.6.0)
gunicorn起動
バックグラウンドプロセスで起動する。
gunicorn falcon_test:api &
起動時の引数は
gunicorn モジュール名:falcon.APIインスタンスの変数名
モジュール名は、pythonスクリプトファイル名の拡張子を省いたもの。
falcon_test.py
api = falcon.API()
起動してみる
※BGプロセスで起動
何かログ出た
gunicorn falcon_test:api [2016-09-15 20:09:08 +0900] [62160] [INFO] Starting gunicorn 19.6.0 [2016-09-15 20:09:08 +0900] [62160] [INFO] Listening at: http://127.0.0.1:8000 (62160) [2016-09-15 20:09:08 +0900] [62160] [INFO] Using worker: sync [2016-09-15 20:09:08 +0900] [62234] [INFO] Booting worker with pid: 62234
curlでアクセスしてみる
gunicornで起動するとfalconアプリケーションで指定したポートは無効になるみたい。
gunicornのデフォルトのポート8000でアクセスする。
curl http://localhost:8000/test_api?key=TEST {'USER-AGENT': 'curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2', 'HOST': 'localhost:8000', 'ACCEPT': '*/*'} {"title": "WebAPIテスト", "tags": [{"バージョン": [], "name": "テスト"}, {"name": "request", "TEST": []}]}
アクセス出来た。
停止するには、プロセス番号を特定してkill
2つプロセスあるけど・・
ps ax|grep gunicorn 62676 pts/0 S 0:00 /root/.pyenv/versions/3.5.0/bin/python3.5 /root/.pyenv/versions/3.5.0/bin/gunicorn falcon_test:api 62750 pts/0 S 0:00 /root/.pyenv/versions/3.5.0/bin/python3.5 /root/.pyenv/versions/3.5.0/bin/gunicorn falcon_test:api
pkill
pkill gunicorn [2016-09-15 20:13:47 +0900] [62676] [INFO] Handling signal: term [2016-09-15 20:13:47 +0900] [62750] [INFO] Worker exiting (pid: 62750) [2016-09-15 20:13:47 +0900] [62676] [INFO] Shutting down: Master
停止してる事を確認
[root@localhost falcon]# ps ax|grep gunicorn 63106 pts/0 S+ 0:00 grep gunicorn
パット見、falconだけの起動時と何も変わらない。
と思ったら、起動時のオプションで色々指定できるみたい。
参考
http://docs.gunicorn.org/en/stable/settings.html
ポート番号指定して起動
gunicorn -b localhost:4000 falcon_test:api &
workerプロセス4つで起動
2~4の間で指定
gunicorn -w 4 falcon_test:api &
4プロセス起動してる
pstree -a|grep gunicorn | |-grep gunicorn | |-gunicorn /root/.pyenv/versions/3.5.0/bin/gunicorn -w 4 falcon_test:api | | |-gunicorn /root/.pyenv/versions/3.5.0/bin/gunicorn -w 4 falcon_test:api | | |-gunicorn /root/.pyenv/versions/3.5.0/bin/gunicorn -w 4 falcon_test:api | | |-gunicorn /root/.pyenv/versions/3.5.0/bin/gunicorn -w 4 falcon_test:api | | `-gunicorn /root/.pyenv/versions/3.5.0/bin/gunicorn -w 4 falcon_test:api
デーモンプロセスで起動
これ指定すれば&はいらないや。
gunicorn -D falcon_test:api
workerのスレッド数
2-4 x CPUコア数の間で指定
gunicorn --threads 2 falcon_test:api
workerの最大コネクション数
gunicorn --worker-connections 100 falcon_test:api
タイムアウト値、ワーカーが指定した秒数応答無い場合、自動的に再起動されます。
合ってるか怪しい
gunicorn -t 30 falcon_test:api
カレントディレクトリを指定
webアプリケーションのpythonがある場所を指定したりする
gunicorn --chdir /work/falcon/ falcon_test:api
pidファイルを作成
gunicorn -p /tmp/gunicorn.pid falcon_test:api
こんな感じでプロセスIDが出力される
cat /tmp/gunicorn.pid 64695
アクセスログ出力
gunicorn --access-logfile /tmp/gunicorn_access.log falcon_test:api
cat /tmp/gunicorn_access.log 127.0.0.1 - - [15/Sep/2016:22:35:13 +0900] "GET /test_api?key=TEST HTTP/1.1" 200 117 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
エラーログ出力
gunicorn --error-logfile /tmp/gunicorn_error.log falcon_test:api
cat /tmp/gunicorn_error.log [2016-09-15 22:36:16 +0900] [65318] [INFO] Starting gunicorn 19.6.0 [2016-09-15 22:36:16 +0900] [65318] [INFO] Listening at: http://127.0.0.1:8000 (65318) [2016-09-15 22:36:16 +0900] [65318] [INFO] Using worker: sync [2016-09-15 22:36:16 +0900] [65332] [INFO] Booting worker with pid: 65332
ログレベル指定
gunicorn --log-level debug falcon_test:api
他も色々設定あるみたい。
後でヘルプで確認してみる
gunicorn -h