Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 6th, 2003, 11:53 AM
Cuboidz Cuboidz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 11 Cuboidz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
mod_python & MySQLdb question

Consider the following code:

Code:
#test.py

import MySQLdb

def process(req, value):
    db = MySQLdb.connect(host="localhost", user="foo", passwd="bar", db="pythondb")
    cursor = db.cursor()
    
    ... do something with database connection ...   

    return something


When you access this file through a browser with /process?value=something appended, mod_python imports it, executes the process function with value as a parameter and displays whatever you returned. My question is, would it be a good idea to move the part where I connect to the database to module(global) level ? Then it wouldn't be neccesary to reconnect every time the process function is called.

Last edited by Cuboidz : October 6th, 2003 at 11:57 AM.

Reply With Quote
  #2  
Old October 6th, 2003, 03:42 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
It really depends on a few things, how many times are you plan on calling this? If it's more than once then you may want to alter your function to handle just the query and have the connection elsewhere, or pass the connection to the function as an preamiter (i havn't tested any of these idea's so they might not work quite like this)

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old October 6th, 2003, 07:18 PM
Balzi Balzi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 42 Balzi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 41 sec
Reputation Power: 10
Sounds like you need a pconnect() equivalent for Python.

you could build a routine that checks the validity of 'db' or 'cursor' and decides if a new connection is needed.

to answer the question - Yes, I think it is always better to hold one connection open than to re-connect for every single query (or even group of queries).

netytan's idea of connecting external to the routine and passing the handle is a good solution aswell.

Reply With Quote
  #4  
Old October 7th, 2003, 12:36 PM
Cuboidz Cuboidz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 11 Cuboidz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you both for answering. I guess I asked this question because I was afraid that when you keep a connection open per user viewing your page you basically have fewer free connections available at any given time. But maybe it doesn't work this way, I don't know.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > mod_python & MySQLdb question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap