python 3.8 flask apache 2.4 wsgi multiprocessing RuntimeError: fork not supported for subinterpreters

Viewed 766

I am trying to run a code in Ubuntu 20.04, with python 3.8, flask, wsgi and using multiprocessing. I wrote a code that works just fine with python. This is the code:

    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()

The problem is when I try to run it using apache 2.4 with the wsgi module. Here is the .conf and .wsgi files:

      <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>

and

    #! /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'

I use a virtual environment. Through apache and wsgi, the code works fine if I remove the multiprocessing code and also the lines:

      #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

in the .wsgi file.

When running the former code I get the error: RuntimeError: fork not supported for subinterpreters. I read the documentation an I find nothing. I also check the python 3.8's bug bugzilla.redhat.com/show_bug.cgi?id=1745894 and it seems that it was solved... Its been a week and I got nothing.. Any Ideas?

1 Answers

from https://github.com/GrahamDumpleton/mod_wsgi/issues/614#issuecomment-691565686

Python 3.8 started to introduce restrictions on what you can do in Python sub interpreters. Python sub interpreters cause various other issues with third party Python modules that aren't designed to work in sub interpreters. For that reason it has been long recommended to avoid them when using mod_wsgi.

To do that, ensure you are using mod_wsgi daemon mode, with only one WSGI application delegated to any mod_wsgi daemon process group. Then force the use of the main Python interpreter context for the WSGI application.

You should delve into the documentation, but in short, use something like the following:

Outside of VirtualHost. This disabled embedded mode to ensure you are using daemon mode.

WSGIRestrictEmbedded On

Inside of VirtualHost (presuming you are using them).

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

Related