0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Flask/uWSGI備忘録

Last updated at Posted at 2022-08-10

Flask

pythonでシンプルなWebAPIが実装できる。
単体でWebサーバにもなれるが、WerkzeugというWSGIモジュールで実装されているため、後述のWSGIの一つとして使える。

Web Server Gateway Interface

WebサーバとWebアプリケーションをつなぐものというイメージ。PEP 3333でその仕様が決められている。
それ単体で動くというより、apacheやnginxにアップロードして使うことを目的としている。

uWSGI

Flaskで作ったWSGIはGoogle App Engine等クラウドにホストする方法もありますが、uWSGIを使いオンプレでホストすることもできます。
c,c++で作られているようです。かなりシンプルなので個人的に好みです。Windowsはサポートされていません。

起動方法

まず設定ファイルを作る。設定ファイルの詳細はここから

uwsgi.ini
[uwsgi]
# wsgiファイル
wsgi-file=app.py
callable=app
# アクセス許可ホスト:ポート
http=0.0.0.0:5050
uid = [user1]
gid = [user1]
daemonize = /home/user1/yourapp/app.log
# pidファイルの位置を指定
pidfile = /home/user1/yourapp/app.pid
# 前回異常終了した場合、起動時にpidファイルをクリア
vacuum = true
起動例
uwsgi uwsgi.ini
終了方法
$ kill -INT `cat /home/user1/yourapp/app.pid`
or
$ uwsgi --stop /home/user1/yourapp/app.pid

公式サイトでは、443等WellKnownポートを使いたい場合は、nginxかApacheを使えと言っています。

起動確認
yusuke@nyao:~/yourapp
$ ps ux -L | grep wsgi | grep -v grep
user1   24062 24062  0.0    3  0.5 890896  5308 ?        Sl    8月03   0:03 uwsgi uwsgi.ini
user1   24062 24067  0.0    3  0.5 890896  5308 ?        Sl    8月03   0:00 uwsgi uwsgi.ini
user1   24062 24077  0.0    3  0.5 890896  5308 ?        Sl    8月03   0:00 uwsgi uwsgi.ini
user1   24063 24063  0.0    1  0.0  53712   436 ?        S     8月03   0:00 uwsgi uwsgi.ini

余談ですがps -Lでスレッド単位で表示されます。

apache-flask

uwsgiを経由せずにapacheのlibapache2-mod-wsgi-py3というプラグインでapache-flaskのアーキテクチャも構成してみましたがscikit-learnをimportするとレスポンスがなくなるという現象が起きました。
このプラグインは情報が少なく利用者も少なそうなので見送りました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?