Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Avoid Connecting To MySQL Multiple Times (http://www.programmingforums.org/showthread.php?t=14551)

Sane Nov 21st, 2007 6:52 PM

Avoid Connecting To MySQL Multiple Times
 
I have a project of .cgi scripts. This is a quick outline of how the files are being mapped together:

:

index.py

    import header
        <Display some header HTML>

    import mysqllib
        <Connect to MySQL and assign the instance to a local variable>

    <Display some information via variable mysqllib.db>
   
    import footer
        import mysqllib
            <Connect to MySQL and assign the instance to a local variable>

        <Display some information via variable mysqllib.db>



The problem should be apparent: both the main page and the footer connects to MySQL. How can I save time and use the same connection to MySQL?

I don't want to connect twice. And I don't want to remap any of the files, because this way makes most sense for how things need to be pieced together.

I have tried:
:

Set a global variable in index.py using globals()
"import footer"
    In footer.py, access the global variable using globals()
    The global variable does not exist


Therefore the global scope does not get passed into a file that is imported.

Your solution?

Jimbo Nov 21st, 2007 7:24 PM

Re: Avoid Connecting To MySQL Multiple Times
 
Is there a way you could have index.py (or an equivalent outermost structure) keep track of the connection and the inner items could query the outer structure for a connection? I haven't used Python much at all, so I don't know if or how that would work out...

Dameon Nov 21st, 2007 7:50 PM

Re: Avoid Connecting To MySQL Multiple Times
 
pconnect ('persistent' connect).

Specifics depend on whatever library you happen to be using.

Sane Nov 21st, 2007 8:22 PM

Re: Avoid Connecting To MySQL Multiple Times
 
Quote:

Originally Posted by Jimbo (Post 137324)
Is there a way you could have index.py (or an equivalent outermost structure) keep track of the connection and the inner items could query the outer structure for a connection? I haven't used Python much at all, so I don't know if or how that would work out...

That's exactly what I'm trying to figure out. Haha. The problem is an inner scope can't see the calling file's global scope.


Quote:

Originally Posted by Dameon (Post 137326)
pconnect ('persistent' connect).

Specifics depend on whatever library you happen to be using.

There is no pconnect in the Python MySQL library, and even if there were, I want to avoid even trying to connect a second time. I don't even want to have to include my mysqllib.py wrapper and the MySQLdb module twice.

Sane Nov 23rd, 2007 1:42 PM

Re: Avoid Connecting To MySQL Multiple Times
 
Too bad Arevos hasn't been posting lately. I know he'd know the way in a second flat. :icon_smile:


All times are GMT -5. The time now is 3:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC