2019年8月24日土曜日

Flask実行中エラーを解消する方法メモ

概要

Flask実行中に「OSError: [Errno 98] Address already in use」が出た際の解消方法メモです。

用語

・Ubuntu:LinuxのOS。
・Python:機械学習プログラミングに人気のプログラミング言語。
・Flask:簡易にpythonサーバを構築できるOSSフレームワーク。

事前準備(前提とする環境)

・Ubuntu14.04
・Python3(3.7.2)
・Flask 1.0.2

内容

Flaskを用いたwebサーバーを稼働させていて、一旦中止した後に再度実行する(開発中などはよく起きると思います)と、そのままでは再実行出来ません(下記)。

vagrant@vagrant-ubuntu-trusty-64:~/vagrant_data$ python3 sample.py
 * Serving Flask app "sample" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
^Z
[1]+  Stopped                 python3 sample.py
vagrant@vagrant-ubuntu-trusty-64:~/vagrant_data$ python3 sample.py
 * Serving Flask app "sample" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "sample.py", line 11, in 
    app.run(host='0.0.0.0')
  File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 943, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 990, in run_simple
    inner()
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 943, in inner
    fd=fd,
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 786, in make_server
    host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/serving.py", line 679, in __init__
    HTTPServer.__init__(self, server_address, handler)
  File "/usr/lib/python3.4/socketserver.py", line 430, in __init__
    self.server_bind()
  File "/usr/lib/python3.4/http/server.py", line 133, in server_bind
    socketserver.TCPServer.server_bind(self)
  File "/usr/lib/python3.4/socketserver.py", line 444, in server_bind
    self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use

これを解決するためには、linuxコマンドを用いてFlaskサーバーのプロセスをkillする必要があります。

そのために、まずは「プロセスID」を調べます。下記例では、プロセスIDは「1990」です。

vagrant@vagrant-ubuntu-trusty-64:~/vagrant_data$ ps -a
  PID TTY          TIME CMD
 1990 pts/0    00:00:00 python3
 1994 pts/0    00:00:00 ps

引き続き、killコマンドでプロセスを削除したいところですが、普通のkillではプロセス削除出来ません。

vagrant@vagrant-ubuntu-trusty-64:~/vagrant_data$ kill 1990
vagrant@vagrant-ubuntu-trusty-64:~/vagrant_data$ ps -a
  PID TTY          TIME CMD
 1990 pts/0    00:00:00 python3
 1995 pts/0    00:00:00 ps

なので、強制終了を行います。

vagrant@vagrant-ubuntu-trusty-64:~/vagrant_data$ kill -9 1990
[1]+  Killed                  python3 sample.py

これで正しくプロセス削除が出来ており、Flaskサーバーを再起動出来るようになります。

vagrant@vagrant-ubuntu-trusty-64:~/vagrant_data$ ps -a
  PID TTY          TIME CMD
 1996 pts/0    00:00:00 ps
vagrant@vagrant-ubuntu-trusty-64:~/vagrant_data$ python3 sample.py
 * Serving Flask app "sample" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

0 件のコメント:

コメントを投稿

FX自動トレード_2021年6月実績

      概要 2021年6月が終わり、FX自動トレードによる収益実績を取り纏めましたので振り返ってみたいと思います。 2021年6月結果 2021年6月の実績は、実利利益額:\66,761、実績利益率は1.42%でした。 今月もボラが全く出ず。利益率の連続過去最低記録更新は免...