1

I am relatively new to Python and Python web application development. Currently I am creating a hello world application in Python using mod_wsgi

Here are my configurations.

Apache configuration

<VirtualHost *:80>
    ServerName mysite.com
    DocumentRoot /var/www/mysite

    WSGIDaemonProcess mysite threads=5
    WSGIScriptAlias / /var/www/mysite/mysite.wsgi
    WSGIProcessGroup mysite

    <Directory /var/www/mysite>
        WSGIProcessGroup mysite
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

mysite.wsgi

import os
import sys

path='/var/www/mysite'
if path not in sys.path:
    sys.path.append(path)

import mysite.app

application = mysite.app.App()

app.py

import mysite.log as log

logger = log.custom_logger('root')
logger.debug('I am included only once')

class App:

    """ 
    This Class is responsible
    """  
    def __init__(self):
        logger.debug('I will be called only after apache restart')          

    """
    WSGI module will call this function by default    
    """
    def __call__(self, environ, start_response):
        logger.debug('I will be invoked for every request')    

        # Do some stuff here
        start_response(response_state, response_header)          
        return [response]

Problem: I am not able to see logs inside __init__ and log which is outside in app.py.

Output

First time run after restarting apache

DEBUG - app - I am included only once

DEBUG - app - I will be called only after apache restart

DEBUG - app - I will be invoked for every request

When I refresh the page in browser

DEBUG - app - I will be invoked for every request

What is happening? I am aware that __init__ is not constructor, application objects are caching somewhere? whats the story.

2
  • Just pointing, did you notice that the class name is App and you're instantiating APP? Commented May 8, 2013 at 4:44
  • Thanks @Bibhas, I have updated Commented May 8, 2013 at 4:47

1 Answer 1

2

Your code doesn't even match the messages logged.

Ignoring that, the module is imported only once when it is first loaded.

The __init__() is called only once when:

application = mysite.app.APP()

runs at the time of import. The __call__() is then run on every request.

So yes, stuff is cached within a process and is reused on subsequent requests.

IOW, things are not reloaded on every request like PHP.

So not sure what the problem is.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I have just updated the logs, I am new to python, not sure how it will render.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.