S
S
sha2562019-01-07 04:58:33
Python
sha256, 2019-01-07 04:58:33

How to access a global variable from a service in python?

Friends, hello everyone!
I am writing my first web service in python, using the spyne -> mod_wsgi -> apache bundle, I raised the part that is responsible for communicating with the application. The application itself had to be taken out of mod_wsgi, because. each request reloads everything again and wildly slows down responses. How to move the application into RAM so that it can be launched once and only accessed through mod_wsgi?
Now I start the script in interactive mode:

def startup():
      global model
      from sklearn.externals import joblib
      model= joblib.load('model')
      print('Model_loaded')

But from mod_wsgi I can't reach the variable:
global model
I would be grateful for links to examples!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sha256, 2019-01-07
@sha256

The problem is treated by mod_wsgi internal daemonization, here is an example of a working Apache config:

<VirtualHost *:80>

    ServerName get
    
    WSGIDaemonProcess pybsk processes=1 threads=5
    WSGIScriptAlias /Get /var/www/python/pymain.py
    
    WSGICallableObject 'application'
    LogLevel info
    DocumentRoot /var/www/python

    <Directory /var/www/python>
        WSGIProcessGroup get
        WSGIApplicationGroup %{GLOBAL}
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>
    <IfDefine MOD_WSGI_LOAD_PYTHON_DYLIB>
        LoadFile ''
    </IfDefine>
    
    WSGIMapHEADToGET Auto

</VirtualHost>

P
Pasechnik Kuzmich, 2019-01-07
@Hivemaster

Global variables are evil.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question