Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 21st, 2007, 5:52 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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?
Sane is offline   Reply With Quote
Old Nov 21st, 2007, 6:24 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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...
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Nov 21st, 2007, 6:50 PM   #3
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Re: Avoid Connecting To MySQL Multiple Times

pconnect ('persistent' connect).

Specifics depend on whatever library you happen to be using.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Nov 21st, 2007, 7:22 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Avoid Connecting To MySQL Multiple Times

Quote:
Originally Posted by Jimbo View Post
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 View Post
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 is offline   Reply With Quote
Old Nov 23rd, 2007, 12:42 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Connecting MySQL and PHP titaniumdecoy PHP 10 Feb 25th, 2008 7:47 PM
MySQl simple problem paulchwd Other Web Development Languages 7 Feb 27th, 2007 10:31 AM
Connecting to MySQL through VB6 wrestlenovi Visual Basic 3 May 25th, 2006 8:06 PM
Tutorial - Using MySQL in C# Darkhack C# 12 Jan 17th, 2006 9:28 AM
Simple Perl / MySQL Problem.. pls help! domquemo Perl 0 Jan 11th, 2006 4:08 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:03 PM.

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