Need help on designing a project

Steve Holden steve at holdenweb.com
Fri Dec 2 06:34:45 EST 2005


Mardy wrote:
> Hi all,
>   I'm starting to think the way I've implemented my program
> (http://www.mardy.it/eligante) is all wrong.
> Basically, what I want is a web application, which might run as CGI
> scripts in apache (and this is working) or even as a standalone
> application, in which case it would use it's own internal webserver.
> 
> The question is about this homemade webserver: right now it's a slightly
> modified version of the standard CGIHTTPServer module. Since I know all my
> CGIs are python scripts, I thought that performance would be best if they
> are executed with the execfile command, in the same process as the
> webserver.
> 
> This works, but my problem is that SQL connections (MySQL or sqlite) don't
> get closed when the script execution finishes, and at the next execution
> of a CGI they may lock the database (this is especially true with sqlite,
> but even mysql on Windows gave me these problems).
> 
> I tryed to call atexit.register() from inside the CGI script (in order to
> close the connection) but my atexit function get called only when the
> webserver itself exits.
> 
> 
> So, I'd like to know if there is a quick solution, or if I have to rewrite
> the whole mechanism (maybe using some existing framework? which one?).
> 
> 
> What I care most, is the ease of installation and use of my program (and
> portability). That's why I'm not contented with apache.
> 
> 
The logical solution to your problem appears to be to explicitly close 
the database connections at the end of each CGI script. Is there some 
reason you can't do this?

Note that if you are using execfile()then the best structure for your 
scripts would be something like:

conn = db.open(....)
try:
     #do CGI stuff
finally:
     conn.close()

to make sure that the connection is always closed. Would this help, do 
you think?

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list