Need help on designing a project

Discussion in 'Python' started by Mardy, Dec 1, 2005.

  1. Mardy

    Mardy Guest

    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.


    --
    Saluti,
    Mardy
    http://interlingua.altervista.org
     
    Mardy, Dec 1, 2005
    #1
    1. Advertising

  2. Mardy

    Steve Holden Guest

    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/
     
    Steve Holden, Dec 2, 2005
    #2
    1. Advertising

  3. Mardy

    Mardy Guest

    Le die Fri, 02 Dec 2005 11:34:45 +0000, Steve Holden ha scribite:
    > 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()


    That was of great help! Thanks!


    --
    Saluti,
    Mardy
    http://interlingua.altervista.org
     
    Mardy, Dec 4, 2005
    #3
  4. Mardy

    Magnus Lycka Guest

    Mardy wrote:
    > 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).


    You should know that there is a severe performance penalty in
    opening and closing database onnections. I actually did a
    measurement on something similar yesterday, and with open/close
    with each database operation, I got 50 operations per second.
    If I only opened and closed once, I could manage 1000 operations
    per second.

    You might want to have a look at something like
    http://sqlrelay.sourceforge.net/
     
    Magnus Lycka, Dec 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,020
  2. =?Utf-8?B?dHBlcnJp?=

    Need help designing a way to flip through articles

    =?Utf-8?B?dHBlcnJp?=, Jun 27, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    398
    Curt_C [MVP]
    Jun 27, 2005
  3. Tony Girgenti

    Need help designing XML Dataset

    Tony Girgenti, Sep 16, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    430
    =?Utf-8?B?RGFuaWVsIEJyaXR0YWluIER1Z2dlcg==?=
    Sep 24, 2006
  4. checoo
    Replies:
    5
    Views:
    815
    Jagger
    Jun 1, 2007
  5. C++ Hell

    Need help in designing a quiz

    C++ Hell, Apr 19, 2006, in forum: C++
    Replies:
    16
    Views:
    619
    Jerry Coffin
    Apr 26, 2006
Loading...

Share This Page