在 Azure 应用服务上使用自定义部署后操作时如何启动 Gunicorn

问题描述 投票:0回答:1

我正在尝试将 Azure 应用服务上的部署后操作与 Flask 应用程序一起使用。我按照 this 堆栈溢出帖子来完成部署后操作,但这会导致容器无法启动并出现以下错误:

Container has finished running with exit code: 0
。根据this,这意味着容器由于没有连续运行而已运行完成,所以我认为这意味着我还需要在部署后操作结束时通过gunicorn启动Flask应用程序。

因此,我按照

this
在我的/home/site/deployments/tools/deploy.sh

的末尾添加了此内容

gunicorn --bind=0.0.0.0 --timeout 600 --workers=3 应用程序:应用程序

但这会导致这个错误:

ModuleNotFoundError: No module named 'app'

我的 app.py 位于我的部署的根目录中,我按照

this
应用服务启动 Flask 教程使用 app = Flask(__name__),所以我不确定哪里出错了。当容器部署时,代码会被复制到类似
/tmp/8dca0e6e81ee3cf
的位置,所以我不确定是否必须在那里神奇地改变方向;这是不可能的,因为目录在应用程序的每次启动时都会发生变化。我哪里错了?

错误日志:

2024-07-10T15:21:31.7220801Z [2024-07-10 15:21:31 +0000] [715] [ERROR] Exception in worker process
2024-07-10T15:21:31.7221200Z Traceback (most recent call last):
2024-07-10T15:21:31.7221276Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
2024-07-10T15:21:31.7221309Z     worker.init_process()
2024-07-10T15:21:31.7221342Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 134, in init_process
2024-07-10T15:21:31.7221375Z     self.load_wsgi()
2024-07-10T15:21:31.7221407Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2024-07-10T15:21:31.7221435Z     self.wsgi = self.app.wsgi()
2024-07-10T15:21:31.7221463Z                 ^^^^^^^^^^^^^^^
2024-07-10T15:21:31.7221523Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/base.py", line 67, in wsgi
2024-07-10T15:21:31.7221552Z     self.callable = self.load()
2024-07-10T15:21:31.7221580Z                     ^^^^^^^^^^^
2024-07-10T15:21:31.7221612Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2024-07-10T15:21:31.7221642Z     return self.load_wsgiapp()
2024-07-10T15:21:31.7221670Z            ^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.7221703Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2024-07-10T15:21:31.7221731Z     return util.import_app(self.app_uri)
2024-07-10T15:21:31.7221784Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.7221816Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/util.py", line 371, in import_app
2024-07-10T15:21:31.7221845Z     mod = importlib.import_module(module)
2024-07-10T15:21:31.7221874Z           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.7221908Z   File "/opt/python/3.12.2/lib/python3.12/importlib/__init__.py", line 90, in import_module
2024-07-10T15:21:31.7221938Z     return _bootstrap._gcd_import(name[level:], package, level)
2024-07-10T15:21:31.7221968Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.7221998Z   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
2024-07-10T15:21:31.7222050Z   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
2024-07-10T15:21:31.7222081Z   File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
2024-07-10T15:21:31.7222110Z ModuleNotFoundError: No module named 'app'
2024-07-10T15:21:31.7222139Z [2024-07-10 15:21:31 +0000] [715] [INFO] Worker exiting (pid: 715)
2024-07-10T15:21:31.7415049Z [2024-07-10 15:21:31 +0000] [716] [INFO] Booting worker with pid: 716
2024-07-10T15:21:31.7840966Z [2024-07-10 15:21:31 +0000] [716] [ERROR] Exception in worker process
2024-07-10T15:21:31.7841281Z Traceback (most recent call last):
2024-07-10T15:21:31.7841356Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
2024-07-10T15:21:31.7841392Z     worker.init_process()
2024-07-10T15:21:31.7841424Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 134, in init_process
2024-07-10T15:21:31.7841451Z     self.load_wsgi()
2024-07-10T15:21:31.7841481Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2024-07-10T15:21:31.7841514Z     self.wsgi = self.app.wsgi()2024-07-10T15:21:31.7841541Z                 ^^^^^^^^^^^^^^^
2024-07-10T15:21:31.7841571Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/base.py", line 67, in wsgi
2024-07-10T15:21:31.7841618Z     self.callable = self.load()
2024-07-10T15:21:31.7841650Z                     ^^^^^^^^^^^
2024-07-10T15:21:31.7841681Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2024-07-10T15:21:31.7841707Z     return self.load_wsgiapp()
2024-07-10T15:21:31.7841734Z            ^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.7841766Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2024-07-10T15:21:31.7841793Z     return util.import_app(self.app_uri)
2024-07-10T15:21:31.7841819Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.7841867Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/util.py", line 371, in import_app
2024-07-10T15:21:31.7841896Z     mod = importlib.import_module(module)
2024-07-10T15:21:31.7841924Z           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.7841953Z   File "/opt/python/3.12.2/lib/python3.12/importlib/__init__.py", line 90, in import_module
2024-07-10T15:21:31.8196675Z     return _bootstrap._gcd_import(name[level:], package, level)
2024-07-10T15:21:31.8201442Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.8204620Z   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
2024-07-10T15:21:31.8209799Z   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
2024-07-10T15:21:31.8213087Z   File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
2024-07-10T15:21:31.8216209Z ModuleNotFoundError: No module named 'app'
2024-07-10T15:21:31.8219509Z [2024-07-10 15:21:31 +0000] [716] [INFO] Worker exiting (pid: 716)
2024-07-10T15:21:31.8292405Z [2024-07-10 15:21:31 +0000] [717] [INFO] Booting worker with pid: 717
2024-07-10T15:21:31.8810430Z [2024-07-10 15:21:31 +0000] [717] [ERROR] Exception in worker process
2024-07-10T15:21:31.8811034Z Traceback (most recent call last):
2024-07-10T15:21:31.8811083Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
2024-07-10T15:21:31.8811148Z     worker.init_process()
2024-07-10T15:21:31.8811181Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 134, in init_process
2024-07-10T15:21:31.8811209Z     self.load_wsgi()
2024-07-10T15:21:31.8811239Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2024-07-10T15:21:31.8811265Z     self.wsgi = self.app.wsgi()
2024-07-10T15:21:31.8811291Z                 ^^^^^^^^^^^^^^^
2024-07-10T15:21:31.8811324Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/base.py", line 67, in wsgi
2024-07-10T15:21:31.8811351Z     self.callable = self.load()
2024-07-10T15:21:31.8811396Z                     ^^^^^^^^^^^
2024-07-10T15:21:31.8811427Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2024-07-10T15:21:31.8811455Z     return self.load_wsgiapp()
2024-07-10T15:21:31.8811482Z            ^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.8811512Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2024-07-10T15:21:31.8811539Z     return util.import_app(self.app_uri)
2024-07-10T15:21:31.8811567Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.8811598Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/util.py", line 371, in import_app
2024-07-10T15:21:31.8811647Z     mod = importlib.import_module(module)
2024-07-10T15:21:31.8811675Z           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.8811705Z   File "/opt/python/3.12.2/lib/python3.12/importlib/__init__.py", line 90, in import_module
2024-07-10T15:21:31.8811734Z     return _bootstrap._gcd_import(name[level:], package, level)
2024-07-10T15:21:31.8811762Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:31.8811790Z   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
2024-07-10T15:21:31.8811820Z   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
2024-07-10T15:21:31.8811848Z   File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
2024-07-10T15:21:31.8811897Z ModuleNotFoundError: No module named 'app'
2024-07-10T15:21:31.9068132Z [2024-07-10 15:21:31 +0000] [717] [INFO] Worker exiting (pid: 717)
2024-07-10T15:21:32.2200387Z [2024-07-10 15:21:32 +0000] [714] [ERROR] Worker (pid:715) exited with code 3
2024-07-10T15:21:32.2379308Z [2024-07-10 15:21:32 +0000] [714] [ERROR] Worker (pid:716) exited with code 3
2024-07-10T15:21:32.2385166Z Traceback (most recent call last):
2024-07-10T15:21:32.2390468Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 209, in run
2024-07-10T15:21:32.2396694Z     self.sleep()
2024-07-10T15:21:32.2399813Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 360, in sleep
2024-07-10T15:21:32.2571431Z     ready = select.select([self.PIPE[0]], [], [], 1.0)
2024-07-10T15:21:32.2576312Z             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:32.2580328Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
2024-07-10T15:21:32.2589456Z     self.reap_workers()
2024-07-10T15:21:32.2595316Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 530, in reap_workers
2024-07-10T15:21:32.2603327Z     raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2024-07-10T15:21:32.2684416Z gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2024-07-10T15:21:32.2687674Z
2024-07-10T15:21:32.2690949Z During handling of the above exception, another exception occurred:
2024-07-10T15:21:32.2693934Z
2024-07-10T15:21:32.2698294Z Traceback (most recent call last):
2024-07-10T15:21:32.2701064Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 662, in kill_worker
2024-07-10T15:21:32.2860795Z     os.kill(pid, sig)
2024-07-10T15:21:32.2861212Z ProcessLookupError: [Errno 3] No such process
2024-07-10T15:21:32.2861251Z
2024-07-10T15:21:32.2861284Z During handling of the above exception, another exception occurred:
2024-07-10T15:21:32.2861313Z
2024-07-10T15:21:32.2861374Z Traceback (most recent call last):
2024-07-10T15:21:32.2861410Z   File "/tmp/8dca0e6e81ee3cf/antenv/bin/gunicorn", line 8, in <module>
2024-07-10T15:21:32.2861440Z     sys.exit(run())
2024-07-10T15:21:32.2861469Z              ^^^^^
2024-07-10T15:21:32.2861503Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
2024-07-10T15:21:32.2861538Z     WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]", prog=prog).run()
2024-07-10T15:21:32.2861794Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/base.py", line 236, in run
2024-07-10T15:21:32.2861823Z     super().run()
2024-07-10T15:21:32.2861877Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/app/base.py", line 72, in run
2024-07-10T15:21:32.2861908Z     Arbiter(self).run()
2024-07-10T15:21:32.2862115Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 229, in run
2024-07-10T15:21:32.2862149Z     self.halt(reason=inst.reason, exit_status=inst.exit_status)
2024-07-10T15:21:32.2862183Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 342, in halt
2024-07-10T15:21:32.2862214Z     self.stop()
2024-07-10T15:21:32.2862248Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 393, in stop
2024-07-10T15:21:32.2862278Z     self.kill_workers(sig)
2024-07-10T15:21:32.2862331Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 652, in kill_workers
2024-07-10T15:21:32.2871261Z     self.kill_worker(pid, sig)
2024-07-10T15:21:32.2879052Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 668, in kill_worker
2024-07-10T15:21:32.2885088Z     self.cfg.worker_exit(self, worker)
2024-07-10T15:21:32.2889894Z     ^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:32.2893789Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/config.py", line 67, in __getattr__
2024-07-10T15:21:32.2976486Z     return self.settings[name].get()
2024-07-10T15:21:32.2981331Z            ^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-10T15:21:32.2984913Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/config.py", line 308, in get
2024-07-10T15:21:32.2990859Z     def get(self):
2024-07-10T15:21:32.2993990Z
2024-07-10T15:21:32.3152847Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
2024-07-10T15:21:32.3153153Z     self.reap_workers()
2024-07-10T15:21:32.3153205Z   File "/tmp/8dca0e6e81ee3cf/antenv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 530, in reap_workers
2024-07-10T15:21:32.3153241Z     raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2024-07-10T15:21:32.3153278Z gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2024-07-10T15:21:32.5498266Z Container has finished running with exit code: 1.
2024-07-10T15:21:32.5548285Z Container is terminating. Grace period: 5 seconds.
2024-07-10T15:21:32.5698740Z Stop and delete container. Retry count = 0
2024-07-10T15:21:32.5704224Z Stopping container: gmtpythonfunctionapp_924ed61c.
2024-07-10T15:21:32.8155945Z Deleting container: gmtpythonfunctionapp_924ed61c. Retry count = 0
2024-07-10T15:21:33.8339373Z Container spec TerminationMessagePolicy  path
2024-07-10T15:21:33.8406716Z Container is terminated. Total time elapsed: 1284 ms.
python azure flask deployment azure-web-app-service
1个回答
0
投票

使用这个让它工作:

cd $APP_PATH
gunicorn --bind 0.0.0.0 --timeout 600 app:app

不确定哪个部分修复了它,只是很高兴它现在可以工作了;我讨厌 Azure 文档。

© www.soinside.com 2019 - 2024. All rights reserved.