我有一个具有以下简化结构的django应用程序:
in_web_server/
├── in_web_server/
│ ├── __init__.py
│ └── wsgi.py
├── in_web_app/
| ├── __init__.py
| └── views/
| └── ...
└── manage.py
这里是wsgi.py
的内容:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
这里in_web_server/__init__.py
:
from uwsgi_tasks import set_uwsgi_callbacks
set_uwsgi_callbacks()
应用程序正在带有以下uwsgi.ini
文件的docker容器中运行:
[uwsgi]
http = 0.0.0.0:8000
# Django-related settings
# the base directory (full path)
chdir = /app
# Django's wsgi file
module = in_web_server.wsgi:application
static-map = /static=/app/static
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# clear environment on exit
vacuum = true
# spooler setup
spooler = /spooler
spooler-processes = 2
spooler-frequency = 10
我正在尝试使用uWSGI假脱机程序运行异步功能。问题是调用该函数时,出现以下异常:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/uwsgi_tasks/tasks.py", line 70, in manage_spool_request
return task.execute_now()
File "/usr/local/lib/python3.7/site-packages/uwsgi_tasks/tasks.py", line 361, in execute_now
result = super(SpoolerTask, self).execute_now()
File "/usr/local/lib/python3.7/site-packages/uwsgi_tasks/tasks.py", line 218, in execute_now
setattr(self.function, self.attr_name, self)
File "/usr/local/lib/python3.7/site-packages/uwsgi_tasks/tasks.py", line 209, in function
self._function = load_function(self.function_name)
File "/usr/local/lib/python3.7/site-packages/uwsgi_tasks/tasks.py", line 62, in load_function
return import_by_path(func_name)
File "/usr/local/lib/python3.7/site-packages/uwsgi_tasks/utils.py", line 25, in import_by_path
raise ImportError('Failed to import "{}" - {}'.format(dotted_path, ex))
ImportError: Failed to import "in_web_app.views.api.dashboard.async_function" - No module named 'in_web_app.views'
Spooler message ignored: "{'spooler_task_name': b'uwsgi_spoolfile_on_e76f182f0916_16_1_1531996504_1533579817_175328', b'function_name': b'in_web_app.views.api.dashboard.async_function', b'args': b'\x80\x03).', b'kwargs': b'\x80\x03}q\x00.', b'setup': b'\x80\x03}q\x00(X\x0b\x00\x00\x00working_dirq\x01X\x04\x00\x00\x00/appq\x02X\x0b\x00\x00\x00retry_countq\x03K\x00u.'}"
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/uwsgi_tasks/utils.py", line 23, in import_by_path
module = import_module(module_path)
File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 953, in _find_and_load_unlocked
File "", line 219, in _call_with_frames_removed
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 953, in _find_and_load_unlocked
File "", line 219, in _call_with_frames_removed
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'in_web_app.views'
当我将from in_web_app.views.api.dashboard import async_function
添加到我的wsgi.py
文件底部时,该异常消失了。但是,这迫使我在其中添加每个异步函数,这不是很方便的解决方案。我究竟做错了什么?如果假脱机程序不是从wsgi.py
导入的,为什么假脱机程序不能导入该函数?
答案 0 :(得分:0)
问题解决了,方法是将我的项目的根目录添加到uwsgi.ini
中的pythonpath中。默认PYTHONPATH中的.
被忽略了。