python-3.x - python 3.8 flask apache 2.4 wsgi 多处理运

我正在尝试在 Ubuntu 20.04 中运行代码,使用 python 3.8、flask、wsgi 并使用多处理。我写了一段与 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 应用程序委托(delegate)给任何 mod_wsgi 守护进程组。然后强制使用 WSGI 应用程序的主要 Python 解释器上下文。

您应该深入研究文档,但简而言之,使用如下内容:

在 VirtualHost 之外。此禁用嵌入式模式以确保您使用守护程序模式。

WSGIRestrictEmbedded 于

VirtualHost 内部(假设您正在使用它们)。

WSGIDaemonProcess myapp WSGIScriptAlias//some/path/myapp.py process-group=myapp application-group=%{GLOBAL}

关于python-3.x - python 3.8 flask apache 2.4 wsgi 多处理运行时错误 : fork not supported for subinterpreters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63861363/

相关文章:

python-3.x - 安装 github 子模块时,诗歌安装失败并显示 [CalledProce

python - open() 中的整数文件描述符 "0"

python - 无法通过 Pip 安装 TensorFlow

linux - 用 n 步旋转文本文件中的行

python - 在使用 pyspark 和预定义的结构模式读取嵌套的 JSON 时,如何将缺失的列

r - 如何使用 R 中的 rcv 和 networkD3 包在 D3 Sankey 图的节点中添加

scala - 在读取 CSV 时,最后一列在 Spark、Scala 中显示为 Null

variables - 如何在 Pyomo 模型中设置决策变量的初始值

python - 使用 Python 的双向方差分析

google-cloud-platform - 接受 Google 帐户转移请求时如何解决消息 "T