Gunicorn 在 Flask 应用程序的服务器挂钩中出现 "Application object must be callable" 失败,但前提是 运行 作为服务

Gunicorn fails with "Application object must be callable" in server hook for Flask app, but only if run as service

我编写了一个 Flask 应用程序,该应用程序与 Gunicorn 和 运行ning 一起在 Raspberry Pi OS (Buster) 上提供。该应用程序应该 运行 在系统启动时自动作为服务。问题是,应用程序在 运行 作为服务时失败...但仅当 运行 作为服务时...

在我将服务器挂钩引入我的 Gunicorn 配置文件之前,它一直有效。其中有几个,但第一个被调用并因此失败的是:

gunicorn.conf.py:

def on_starting(server):
    import wsgi
    wsgi.on_starting(server)

wsgi.py:

def on_starting(server):
    api_instance = server.app.wsgi()
    shared_memory_manager = Manager()
    api_instance.requestless_variables = shared_memory_manager.dict()
    api_instance.log = server.log
    server.log.info("Loading API...")

具有以下回溯

Traceback (most recent call last):
  File "/home/pi/.local/bin/gunicorn", line 8, in <module>
    sys.exit(run())
  File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/base.py", line 231, in run
    super().run()
  File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 198, in run
    self.start()
  File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 138, in start
    self.cfg.on_starting(self)
  File "/home/pi/nano/manager/src/api/gunicorn.conf.py", line 56, in on_starting
    api_instance = server.app.wsgi()
  File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
  File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/home/pi/.local/lib/python3.8/site-packages/gunicorn/util.py", line 430, in import_app
    raise AppImportError("Application object must be callable.")
gunicorn.errors.AppImportError: Application object must be callable.

如您所见,错误似乎与 api_instance = server.app.wsgi() 有关,它出现在我的每个服务器挂钩中,同样也是我在每个挂钩中的失败点。

绝对最奇怪的事情是 app/Gunicorn 如果直接从终端实例化,它会完美地工作: /home/pi/.local/bin/gunicorn -c /home/pi/nano/manager/src/api/gunicorn.conf.py --bind unix:nano_api.sock --umask 007 但是,如果从以下 service:

实例化,则会产生上述错误
[Unit]
Description=Gunicorn instance serving the Nano API
After=network.target

[Service]
User=pi
Group=www-data
WorkingDirectory=/home/pi/nano/manager/src/api
ExecStart=/home/pi/.local/bin/gunicorn -c /home/pi/nano/manager/src/api/gunicorn.conf.py --bind unix:nano_api.sock --umask 007
[Install]
WantedBy=multi-user.target

任何人都知道可能导致此问题的原因以及如何解决它?非常感谢!

好吧,经过扎实的10个小时的调试,我终于弄明白了问题...

不好意思说这只是命名冲突。我有一个名为“api”的包,其中包含一个名为“api.py”的模块,该模块包含一个名为“api”的 Flask 实例,您猜对了,“api”。

因为我给这三个项目起了不同的名字并固定了我的参考资料,所以一切 运行 都很顺利。