|
Thimios Katsoulis
thkatsou at yahoo.gr
Sat Oct 13 11:06:36 EDT 2007
Hello.
Sorry for my poor english. Here is my mod_python code. This is a simple
URL shorter:
from mod_python import apache, util
import psycopg2 as psycopg
def handler(req):
req.content_type = "text/plain"
url_id = req.args
connection = psycopg.connect("dbname=my_db")
cursor = connection.cursor()
cursor.execute("""SELECT myurl FROM urls WHERE myid=%s""",(url_id))
original_url = cursor.fetchone()[0]
connection.close()
util.redirect(req,original_url)
req.status = apache.DONE
return apache.DONE
This programme connects database everytime, but I want(need) force him
to connect it continously
This script connects database everytime (psycopg.connect("dbname=my_db")
, but I nedd force to stay conneceted with it continously. It is
possible to make that in mod_python ? I have mod_python 3.2.10
Thanks in advance.
rdn
---------------
Yes mod_python can maintain global variables per interpreter, so you can open once the connection
and all subsequent requests will use the opened connection.
You have to declare your connection variable as global e.g. :
def getConn(self):
try:
if _conn == None:
self.openConn()
except NameError:
self.openConn()
return _conn
def openConn(self):
global _conn
_conn = connect(self.ConnStr)
So when you want to access it you call self.getConn().
The _conn global variable will instantiate once for each mod_python apache process.
You can include some apache.log_error calls in the code above to watch
for yourself when the _conn varible gets instantiated and when it is retreived as global.
Please take notice from what I have observed that while you maintain open connections to DB (postgresql too in my case)
you cannot change structure of the tables etc in the DB..
Of course if you are using modules and not objects you have to alter the code to support (removing self ..) modules.
___________________________________________________________
Χρησιμοποιείτε Yahoo!;
Βαρεθήκατε τα ενοχλητικά μηνύματα (spam); Το Yahoo! Mail
διαθέτει την καλύτερη δυνατή προστασία κατά των ενοχλητικών
μηνυμάτων http://login.yahoo.com/config/mail?.intl=gr
|