python-3.x - python 3.8 flask apache 2.4 wsgi multiprocessing RuntimeError: fork not supported for subinterpreters
问题描述
我正在尝试使用 python 3.8、flask、wsgi 并使用多处理在 Ubuntu 20.04 中运行代码。我编写了一个与 python 配合得很好的代码。这是代码:
from flask import Flask
from multiprocessing import Process, Manager
def f(d, l):
d[1] = '1'
d['2'] = 2
d[0.25] = None
l.reverse()
# Function to convert
def listToString(s):
# initialize an empty string
str1 = " "
res = str1.join(list(map(str,s)))
# return string
return res
app = Flask(__name__)
@app.route("/")
def hello():
with Manager() as manager:
d = manager.dict()
l = manager.list(range(10))
p = Process(target=f, args=(d, l))
p.start()
p.join()
print(d)
print(l)
return "Hello world! " + listToString(d)
if __name__ == "__main__":
app.run()
问题是当我尝试使用带有 wsgi 模块的 apache 2.4 运行它时。这是 .conf 和 .wsgi 文件:
<VirtualHost *:80>
# Add machine's IP address (use ifconfig command)
ServerName 192.168.0.7
#Application Configuration. SetEnv command
SetEnv wsgi.multithread True
SetEnv wsgi.multiprocess True
WSGIDaemonProcess testFlask processes=2 threads=15 python-path=/home/israel/server/study:/home/israel/server/env/lib/python3.8/site-packages
# Give an alias to to start your website url with
WSGIScriptAlias /testFlask /home/israel/server/study/my_flask_app.wsgi
WSGIProcessGroup testFlask
<Directory /home/israel/server/study/>
# set permissions as per apache2.conf file
<IfVersion < 2.4>
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.4>
Require all granted
</IfVersion>
Options FollowSymLinks
AllowOverride None
</Directory>
ErrorLog ${APACHE_LOG_DIR}/test-error.log
LogLevel info
CustomLog ${APACHE_LOG_DIR}/test-access.log combined
</VirtualHost>
和
#! /usr/bin/python3.8
python_home = '/home/israel/server/env'
activate_this = python_home + '/bin/activate_this.py'
print("activate dir", activate_this)
#python 3.8
exec(open(activate_this).read(), dict(__file__=activate_this))
import logging
import sys
#checking version
print('systems version {}.{}.{}'.format(sys.version_info[0], sys.version_info[1], sys.version_info[2]))
sys.path.insert(0, '/home/israel/server/study/')
from my_flask_app import app as application
application.secret_key = 'anything you wish'
我使用虚拟环境。通过 apache 和 wsgi,如果我删除多处理代码以及以下行,代码可以正常工作:
#Application Configuration. SetEnv command
SetEnv wsgi.multithread True
SetEnv wsgi.multiprocess True
WSGIDaemonProcess testFlask processes=2 threads=15 python-path=/home/israel/Documents/Classlol/face_Glasses_Fitting_server-quis/study:/home/israel/Documents/Classlol/face_Glasses_Fitting_server-quis/env/lib/python3.8/site-packages
在 .wsgi 文件中。
运行以前的代码时,我收到错误:RuntimeError: fork not supported for subinterpreters。我阅读了文档,但一无所获。我还检查了 python 3.8 的错误bugzilla.redhat.com/show_bug.cgi?id=1745894似乎已经解决了......已经一个星期了,我什么也没得到......有什么想法吗?
解决方案
来自https://github.com/GrahamDumpleton/mod_wsgi/issues/614#issuecomment-691565686
Python 3.8 开始对 Python 子解释器的功能进行限制。Python 子解释器会导致第三方 Python 模块出现各种其他问题,这些模块并非旨在在子解释器中工作。出于这个原因,长期以来一直建议在使用 mod_wsgi 时避免使用它们。
为此,请确保您使用的是 mod_wsgi 守护进程模式,只有一个 WSGI 应用程序委派给任何 mod_wsgi 守护进程组。然后强制为 WSGI 应用程序使用主要的 Python 解释器上下文。
您应该深入研究文档,但简而言之,请使用以下内容:
在虚拟主机之外。此禁用嵌入模式以确保您使用的是守护进程模式。
WSGIRestrict 嵌入
在 VirtualHost 内部(假设您正在使用它们)。
WSGIDaemonProcess myapp WSGIScriptAlias / /some/path/myapp.py process-group=myapp application-group=%{GLOBAL}
推荐阅读
- swift - 如何一次检索关注用户的帖子?
- string - how to use replace on a left join statement
- java - 从 java 8 中的对象列表创建字符串和列表的映射
- r - 当某些 x 值为零时,在 r 中拟合 nls()
- python - Cleaning up after AWS Lambda execution context is closed with Python
- c# - Why are my vectors being reset instead of having their own value?
- swift - 更新非活动窗口
- button - Google Charts Button
- regex - Regex to match variable length, spaces and special chars?
- c++ - 如何确保执行 RVO 而不是复制?