Flask应用程序无法在Azure上启动,连接正在使用中

x6h2sr28  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(103)

我尝试部署一个非常简单的Flask应用程序:

import flask

app = flask.Flask(__name__)

@app.route('/', methods=['GET'])
def home():
    return '<h1>Hello!</h1></p>'

app.run()

我遵循了一些官方教程,在Azure Portal中创建了App Service,使用Local Git解决方案部署了应用程序。应用程序已部署,但当我尝试浏览它时,我得到:
:(应用程序错误。如果您是应用程序管理员,则可以访问诊断资源。
我的日志:

2022-09-22T14:24:17.032341362Z 
2022-09-22T14:24:17.032431764Z   _____                               
2022-09-22T14:24:17.032440964Z   /  _  \ __________ _________   ____  
2022-09-22T14:24:17.032445564Z  /  /_\  \___   /  |  \_  __ \_/ __ \ 
2022-09-22T14:24:17.032449964Z /    |    \/    /|  |  /|  | \/\  ___/ 
2022-09-22T14:24:17.032454364Z \____|__  /_____ \____/ |__|    \___  >
2022-09-22T14:24:17.032458965Z         \/      \/                  \/ 
2022-09-22T14:24:17.032463265Z 
2022-09-22T14:24:17.032467265Z A P P   S E R V I C E   O N   L I N U X
2022-09-22T14:24:17.032471165Z 
2022-09-22T14:24:17.032474965Z Documentation: http://aka.ms/webapp-linux
2022-09-22T14:24:17.032489065Z Python 3.9.7
2022-09-22T14:24:17.032493765Z Note: Any data outside '/home' is not persisted
2022-09-22T14:24:17.180055832Z Starting OpenBSD Secure Shell server: sshd.
2022-09-22T14:24:17.215008000Z App Command Line not configured, will attempt auto-detect
2022-09-22T14:24:17.433349825Z Starting periodic command scheduler: cron.
2022-09-22T14:24:17.442192344Z Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite
2022-09-22T14:24:17.556237878Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2022-09-22T14:24:17.562671538Z Output is compressed. Extracting it...
2022-09-22T14:24:17.564241977Z Build Operation ID: |OgP/jQgIHgU=.1da790b7_
2022-09-22T14:24:17.564259877Z Oryx Version: 0.2.20220825.1, Commit: 24032445dbf7bf6ef068688f1b123a7144453b7f, ReleaseTagName: 20220825.1
2022-09-22T14:24:17.565391205Z Extracting '/home/site/wwwroot/output.tar.gz' to directory '/tmp/8da9ca604221f20'...
2022-09-22T14:24:18.725265722Z App path is set to '/tmp/8da9ca604221f20'
2022-09-22T14:24:19.018530608Z Detected an app based on Flask
2022-09-22T14:24:19.405035911Z Generating `gunicorn` command for 'index:app'
2022-09-22T14:24:19.456995902Z Writing output script to '/opt/startup/startup.sh'
2022-09-22T14:24:19.689506779Z Using packages from virtual environment antenv located at /tmp/8da9ca604221f20/antenv.
2022-09-22T14:24:19.690332099Z Updated PYTHONPATH to ':/opt/startup/app_logs:/opt/startup/code_profiler:/tmp/8da9ca604221f20/antenv/lib/python3.9/site-packages'
2022-09-22T14:24:20.556171311Z [2022-09-22 14:24:20 +0000] [77] [INFO] Starting gunicorn 20.1.0
2022-09-22T14:24:20.595370385Z [2022-09-22 14:24:20 +0000] [77] [INFO] Listening at: http://0.0.0.0:8000 (77)
2022-09-22T14:24:20.601700442Z [2022-09-22 14:24:20 +0000] [77] [INFO] Using worker: sync
2022-09-22T14:24:20.610324257Z [2022-09-22 14:24:20 +0000] [80] [INFO] Booting worker with pid: 80
2022-09-22T14:24:21.265168226Z  * Serving Flask app 'index'
2022-09-22T14:24:21.266054348Z  * Debug mode: on
2022-09-22T14:24:21.300157595Z [31m[1mWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.[0m
2022-09-22T14:24:21.300272498Z  * Running on http://127.0.0.1:5000
2022-09-22T14:24:21.301268023Z [33mPress CTRL+C to quit[0m
2022-09-22T14:24:21.308547204Z  * Restarting with stat
2022-09-22T14:24:21.760007120Z [2022-09-22 14:24:21 +0000] [81] [INFO] Starting gunicorn 20.1.0
2022-09-22T14:24:21.772788238Z [2022-09-22 14:24:21 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:21.773676260Z [2022-09-22 14:24:21 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:22.779581652Z [2022-09-22 14:24:22 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:22.780698179Z [2022-09-22 14:24:22 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:23.781856453Z [2022-09-22 14:24:23 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:23.783124685Z [2022-09-22 14:24:23 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:24.787329534Z [2022-09-22 14:24:24 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:24.788615966Z [2022-09-22 14:24:24 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:25.792686075Z [2022-09-22 14:24:25 +0000] [81] [ERROR] Connection in use: ('0.0.0.0', 8000)
2022-09-22T14:24:25.794038102Z [2022-09-22 14:24:25 +0000] [81] [ERROR] Retrying in 1 second.
2022-09-22T14:24:26.795751866Z [2022-09-22 14:24:26 +0000] [81] [ERROR] Can't connect to ('0.0.0.0', 8000)
2022-09-22T14:24:26.839176431Z [2022-09-22 14:24:26 +0000] [80] [INFO] Worker exiting (pid: 80)
2022-09-22T14:24:26.928820618Z [2022-09-22 14:24:26 +0000] [84] [INFO] Booting worker with pid: 84

它总是发生,启动,端口问题,重新启动,一次又一次。正如我所说,我只是按照教程,没有改变任何端口或IP地址.

ve7v8dk2

ve7v8dk21#

我找到了解决办法。我的代码是开发版本-在日志中是一个警告:
警告:这是一个开发服务器。不要在生产部署中使用它。使用生产WSGI服务器
我修改了Flask at first run: Do not use the development server in a production environment中的代码:

import flask

app = flask.Flask(__name__)

@app.route('/', methods=['GET'])
def home():
    return '<h1>Hello!</h1></p>'

# debug app:
# app.run()

if __name__ == "__main__":
    from waitress import serve
    serve(app, host="0.0.0.0", port=8080)

现在,应用程序已部署到Azure并正常工作。

相关问题