我正在使用 Ubuntu 编写一个网络服务器以托管在 aws ec2 实例中,稍后我会将其链接到前端以进行 POST 和 GET 申请。我执行了与为此项目部署一个简单的 Python Web 服务器 中所示的相同步骤。问题是,当我尝试使用命令
gunicorn -w 3 objDetection:app
时,出现以下错误:
[2023-04-24 21:50:30 +0000] [4826] [INFO] Starting gunicorn 20.1.0
[2023-04-24 21:50:30 +0000] [4826] [INFO] Listening at: http://127.0.0.1:8000 (4826)
[2023-04-24 21:50:30 +0000] [4826] [INFO] Using worker: sync
[2023-04-24 21:50:30 +0000] [4827] [INFO] Booting worker with pid: 4827
[2023-04-24 21:50:30 +0000] [4827] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/lib/python3/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/usr/lib/python3/dist-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/usr/lib/python3/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/usr/lib/python3/dist-packages/gunicorn/util.py", line 384, in import_app
mod = importlib.import_module(module)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/ubuntu/objDetection/__init__.py", line 1, in <module>
import object_detection
ModuleNotFoundError: No module named 'object_detection'
[2023-04-24 21:50:30 +0000] [4827] [INFO] Worker exiting (pid: 4827)
[2023-04-24 21:50:30 +0000] [4826] [INFO] Shutting down: Master
[2023-04-24 21:50:30 +0000] [4826] [INFO] Reason: Worker failed to boot.
为了更好地理解问题,这里是我的项目路径:
...
/home
|
/ubuntu
|
/objDetection --> My project folder, everything is here
|
__pycache__
__init__.py
coco.names
frozen_inference_graph.pb
index.html
object_detection.py --> error
ssd_mobilenet_v3_large_coco_2020_01_04.pbtxt
venv --> Virtual environment, used only to install gunicorn, everything else was installed normally (python3, pip, flask, nginx)
还有我的init.py 文件:
import object_detection # this seems to be the problem
from flask import Flask, request, send_file, render_template
from flask_cors import CORS
# Define Flask and CORS
app = Flask(__name__)
CORS(app)
@app.route('/')
def webpage():
return render_template("index.html")
# Endpoint for receiving an image for processing photo
@app.route('/post-photo', methods=['POST'])
def handle_image():
# Retrieving the POST image formData from frontend
image = request.files.get('image')
# Return error if image wasn't on the formData
if image is None:
return 'Image not found in form', 400
# Saving image locally
image.save('./Images/my-photo.jpg')
# Processing image
object_detection.process_image('./Images/my-photo.jpg')
# Return received image message
return 'Image received successfully!', 200
# Endpoint for sending the processed photo
@app.route('/get-processed-photo', methods=['GET'])
def send_processed_photo():
processed_image_path = 'Images/processed-photo.jpg'
return send_file(processed_image_path, mimetype='image/jpg')
if __name__ == '__main__':
app.run(debug=True)
据我了解,gunicorn 无法找到或无法识别 object_detection.py 的导入。
好心人能帮我解决这个问题吗,我最近开始开发这个项目,但仍然对nginx、gunicorn一无所知。如果有人有知识并且可以解释它,那么解决这个问题并从中学习会很棒。
我安装了python3、pip、flask和nginx。自从我在其他主题中看到有关 gunicorn 的全局路径后,他们创建了一个虚拟环境来在其中安装 gunicorn。我还尝试使用 wsgi 文件,如如何使用 Gunicorn 和 Nginx 部署 Flask(在 Ubuntu 上),但无处可去。
我期待执行命令
gunicorn -w 3 objDetection:app
没有任何错误并让我的网络服务器运行。
PS:对不起,关于 youtube 链接,这是我第一次提出问题和编码这种东西。
不确定我是否做对了,但据我测试,您似乎没有正确执行
gunicorn
命令。
首先,激活虚拟环境:
$ source venv/bin/activate
然后将文件
__init__.py
重命名为app.py
,然后运行:
(venv)$ gunicorn -w 3 main:app
而且,如果您只使用
venv
来安装 gunicorn
,那么您可能会遇到其他依赖项(例如 flask
和 flask_cors
)的问题。如果是这种情况,只需安装它们并再次运行 gunicorn
命令。
希望有帮助!