【发布时间】:2021-06-14 11:03:01
【问题描述】:
我已将一个 Django 项目部署到生产环境,但由于后端脚本中的 python 多进程,该项目无法正确处理请求。问题出在我在网页中的联系表单中,用户应该在其中输入姓名、电子邮件和他们想要发送的消息。一旦他们点击发送消息,就会执行recaptcha,如果返回true,消息将使用Python的多进程通过电子邮件发送。所以这个过程看起来像
用户输入消息表单 -> 消息表单发布 -> reCaptha 检查 -> 如果通过 -> 多进程通过电子邮件发送消息
在开发模式下测试时一切正常,但在使用 apache2 部署时,我收到以下错误
Environment:
Request Method: POST
Request URL: https://www.dimsum.dk/hdnytorv
Django Version: 3.1.3
Python Version: 3.8.5
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/jianwu/HD_website/website/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/jianwu/HD_website/website/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jianwu/HD_website/website/env/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/jianwu/HD_website/website/env/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "/home/jianwu/HD_website/website/index/views.py", line 146, in post
emailObject = sendEmail(form = form,
File "/home/jianwu/HD_website/website/website/Modules/emailMessage.py", line 40, in __init__
p1.start()
File "/usr/lib/python3.8/multiprocessing/process.py", line 121, in start
self._popen = self._Popen(self)
File "/usr/lib/python3.8/multiprocessing/context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "/usr/lib/python3.8/multiprocessing/context.py", line 277, in _Popen
return Popen(process_obj)
File "/usr/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
self._launch(process_obj)
File "/usr/lib/python3.8/multiprocessing/popen_fork.py", line 70, in _launch
self.pid = os.fork()
Exception Type: RuntimeError at /hdnytorv
Exception Value: fork not supported for subinterpreters
当我查看mod_wsgi 的文档时,我意识到这很可能是由于我在后端使用了 Python 的多进程造成的。作为网络服务器,我使用具有以下配置的 apache2
[Wed Mar 17 05:25:51.489125 2021] [core:warn] [pid 50605] AH00111: Config variable ${APACHE_RUN_DIR} is not defined
apache2: Syntax error on line 80 of /etc/apache2/apache2.conf: DefaultRuntimeDir must be a valid directory, absolute or relative to ServerRoot
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2020-08-12T19:46:17
Server's Module Magic Number: 20120211:88
Server loaded: APR 1.6.5, APR-UTIL 1.6.1
Compiled using: APR 1.6.5, APR-UTIL 1.6.1
Architecture: 64-bit
Server MPM:
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/apache2"
-D SUEXEC_BIN="/usr/lib/apache2/suexec"
-D DEFAULT_PIDLOG="/var/run/apache2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="mime.types"
-D SERVER_CONFIG_FILE="apache2.conf"
我不确定如何正确配置 apache2 或 mod_wsgi 来解决这个问题。另外,解决此类问题的最佳做法是什么?我使用多进程的原因是因为我希望给用户一个流畅的网络体验,而不需要等待后端完成可能需要几秒钟的电子邮件发送过程。因此,在使用 recaptcha 验证后,用户会收到一条消息,电子邮件发送成功,而另一个进程在后台处理实际的电子邮件发送。
【问题讨论】:
标签: django apache mod-wsgi django-wsgi