[mod_python] transhandler setup?

Dirk van Oosterbosch, IR labs labs at ixopusada.com
Sun Sep 4 13:08:25 EDT 2005


On 2-sep-05, at 03:39, Graham Dumpleton wrote:

>>
>> <IfModule mod_python.c>
>>      PythonPath "sys.path + ['/var/www/python/']"
>>      PythonTransHandler translate
>
> Try adding here in addition to the above:
>
>   AddHandler mod_python .py
>   PythonHandler main
>
> Ie., rather than trying to use req.add_handler(). What happens then?

Well, I guess it works in the end.

Sorry, my bad.
I mixed up two versions of  main.py  I had laying around. (I modified 
my translate.py so the  req.filename  becomes  main_from_trans.py , but 
didn't use that name in the  PythonHandler  directive).
It does strike me as strange however, that I have to use   PythonHander 
python_main_file   in the apache configuration, when I have the 
transhandler decide that it should use python_main_file.py as filename. 
That would mean I can't make the transhandler fork its behavior between 
different python files as the ones that should become the filename and 
should be handled in the next phase (handler  that is). (Unless I have 
it fork to different directories and use different <Directory> 
configurations, of course)
...
I mean something like:
-----translate.py:
def transhandler(req):
	...
	if fork == 1:
		req.filename = "/var/www/python/main_alternative_a.py"
	else:
		req.filename = "/var/www/python/main_alternative_b.py"
	return apache.OK
-----
wouldn't work because I would have to say

<IfModule mod_python.c>
      PythonPath "sys.path + ['/var/www/python/']"
      PythonTransHandler translate
</IfModule>

<Directory "/var/www/python">
      AddHandler mod_python .py
      PythonHandler main_alternative_a	---OR---	 PythonHandler 
main_alternative_b
</Directory>

...

Also I found out that if I add
req.add_handler("PythonHandler", "main")
to translate.py it executes the handler from main.py twice (you get a 
double html file) , but only if it can successfully do the first one 
(set by  req.filename = "/var/www/python/main.py" .


But at least I got transhandler working finally.


One last question though on performance and recommendation on design.
I still see those two options as solutions for my design:
1. have everything handled by one main.py file (which decides whether 
or not it should really handle it or just decline and have apache take 
over and which redirects urls for a directory e.g. 
www.myserver.com/folder to var/www/folder/index.html internally if that 
index.html is present)
2. use two phases: transhandler and handler. Have transhandler find out 
if the file (or directory) exists, and if not checks my database if it 
can serve a virtual file or directory index and makes the req.filename 
become a main.py to be handled in the second phase.

>
> For an internal redirect, you probably want:
>
>   req.internal_redirect(req.uri+'/index.html')
>
> This will not send a response back to the browser and it will managed 
> internal
> to Apache.


Question I now have: does anybody know how efficient this internal 
redirect would be in comparison to a database query?
For the point is, that with option 1, all the static directories 
(requests for www.myserver.com/folder) will go through apache twice: 
(It first is handled by handler in main.py which then decides it should 
be www.myserver.com/folder/index.html anyway.
How economically inefficient is that performance wise?
The alternative, option 2, has the downside of two database queries for 
every dynamic page: First transhandler checks whether the url exists in 
the database. Then later, handler again queries the database for this 
url.

Is one of these alternatives much more performance intensive than the 
other?
I know I am comparing apples with pears here. The one is an internal 
redirect handled between mod_python and apache. The other is a database 
query handled my MySQL and MySQLdb (which is also dependent to how the 
database is designed and which field are indexed and so on...). Further 
more the decision between the two options is also dependent on how many 
these scenarios occur: do I have a lot of static index.html files which 
are requested by only their directory, or do I serve mostly dynamic 
pages.
So indeed the decision won't be just a matter of comparing performances 
of the two, but if someone can just give me a "handle"...

Sorry for asking so many questions ...
dirk



-----------------------------
Dirk van Oosterbosch
dirk at ixopusada.com
-----------------------------




More information about the Mod_python mailing list