Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

A Brief Introduction to Apache's mod_python Module

Giving Apache the ability to run Python scripts opens up a wealth of options for eliminating those old CGI scripts and using Python for your Web development tasks—and gives your scripts a big performance boost into the bargain!


advertisement
magine that you are a PHP programmer. You use PHP, because it's simple—but powerful enough to provide you with most of the features you need to develop Web-based applications for your customers. You have heard of or perhaps even played with more powerful languages, such as Python. Most of your development involves building Web applications, so you are concerned about performance, scalability, and flexibility.

The answer to this problem is mod_python, an Apache Web server module that embeds the Python interpreter within the Apache server in a manner similar to PHP. The advantages to this are many, but briefly, embedding the Python interpreter into Apache provides a persistent execution stack that eliminates the start-up penalty associated with running CGI scripts. It also lets you store specific information about the environment and visitors to your application—creating a pseudo-state mechanism for your Web site.

What exactly are we talking about, when we say a persistent execution stack that eliminates the start-up penalty associated with running CGI scripts? The best way to understand the difference between running a CGI application and an embedded code interpreter is to run some tests. As a benchmark I'll use a simple CGI script first, and then run the equivalent script executed within the mod_python environment. I've used the Apache application Apache Bench (ab).

The following simple Web form posts a user's selection from a select list to a simple CGI script. The form sends one value, named "month," to the server, where the CGI script prints out the month within the year 2005.

<title>A simple "get month" script</title> <body> <form method="POST" action="http://localhost/cgi-bin/getmonth.py"> <p> <hr> <br /> Show which month for 2005?: <br /> <select name="month"> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <br /> <input type="submit" value="Show Month"> </form> </body>

A Simple Python CGI Script
On the server, the CGI script extracts the month value from the form data and prints it out.

#!/usr/bin/python # # # A simple script to output a calendar month based off input from a Web form. # # import cgi,calendar print "Content-Type: text/html \n\n" print "<title>A month</title>" print "<body><pre>" form = cgi.FieldStorage() month = form.getvalue("month") try: calendar.prmonth(2005, int(month),2,3) except Exception,e: print "That is not a valid month" print "</body></pre>"

CGI vs. mod_python: Performance Results
Because other machine operations may influence any single result, you must test the application multiple times, taking the mean value over all the executions to get an accurate value. Executing the CGI version 1000 times using 10 connections:



(mean time, across all concurrent requests) Time per request: 77.723 [ms]

Executing an equivalent script within the mod_python environment 1000 times using 10 connections, the execution time drops dramatically:

(mean time, across all concurrent requests) Time per request: 5.808 [ms]


Reviewing the above you can see that the mean time of all concurrent requests for the CGI execution is 77.723 milliseconds; but the mean time of all concurrent requests for the mod_python-enabled script is only 5.8 milliseconds—a huge performance increase.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap