[mod_python] Concurrence and virtual host interpreter python

Pau Freixes pfreixes at milnou.net
Fri Sep 28 05:19:33 EDT 2007

Hi to all, Im new in a list.

Well, this last days I was work for testing and learn how work with multi
thread and multi interpreter environment in embedding python in C
applications, and of course mod_python it's a excellent place for
understand this.

Ok, with my lectures and test with mod_python code, i have a some
questions, somebody can help me for understand this concepts ?

1. I was try multi concurrence in a virtual host python script with this
easy code

def handler(req):

    # Request Content Type
    req.content_type = "text/plain"

    VALUE2 = 1
    for i in range(1,10):
        req.write("Value is %d\r\n" % VALUE2)

        # change global variable
        VALUE2 = VALUE2 + 1;

    req.write("Hellow World")
    return apache.OK

And my surprise it's that apache only process the next request when the
previous request has been served. !!

I don't understand this strange behavior, because mod_python it's made
for accept concurrent threads in same interpreter, and i test a similar
situation  in a c code and it's possible do run some threads at same
time in same interpreter.

time.sleep(5) it's a pretty situation for python core for change the
context and release GIL and assign to this to other thread. yes ?

2. And de second question it's in similar situation, when you reuse a
interpreter for all request to same virtual host, with this code :

import time


def handler(req):
    global VALUE

    # Request Content Type
    req.content_type = "text/plain"

    for i in range(1,10):
        req.write("Value is %d\r\n" % VALUE)

        # change global variable
        VALUE = VALUE + 1;

    req.write("Hellow World")
    return apache.OK

VALUE it's a global variable for de index.py, and subsequent request for
the same url (http://...../index.py) show one incremental value for
VALUE. For example, this two request produced this two results


Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is 6
Value is 7
Value is 8
Value is 9
Hellow World


Value is 10
Value is 11
Value is 12
Value is 13
Value is 14
Value is 15
Value is 16
Value is 17
Value is 18
Hellow World

Ok, this situation it's really and i think its very normal because only
callback to handler it's made with the same interpreter and VALUE is
not re assigned to value 1, may be ?

3. And the most important question, why mod_python not use a separate
interpreters for all request ?

Thks to all and sorry for my large email

More information about the Mod_python mailing list