views:

98

answers:

2

Hello,

PHP provides mysql_connect() and mysql_pconnect() which allow creating both temporary and persistent database connections.

Is there a similar functionality in Python? The environment on which this will be used is lighttpd server with FastCGI.

Thank you!

+4  A: 

If you're using FastCGI, there's no need for "persistent connections", because if you create a connection it is by default persistent, since FastCGI python isn't request based, but constantly running.

This is how FastCGI works in python, to put it short:

1. Run startup code
2. Run request function
3. Wait for new request, then goto step 2.

In PHP/FastCGI this is different, because only the PHP engine is loaded all the time, while the PHP script itself is executed for each request.

1. Start PHP Engine
2. Run script
3. Wait for new request, then goto step 2.

So the difference is that in Python you can define your own initialization. And that's where you put your MySQL connection. :)

Tor Valamo
I am not quite sure how this works, which is probably because I am very new to both Python and PHP. Even with Python each request to the webserver is handeled independently, how is the connection maitained in this case? Is it the case of MySQL module somehow being caching the connection and reusing the same one when I try to reconnect to it?
Alex
See my edited post. I hope it makes sense now.
Tor Valamo
Thank you! It makes sense now.
Alex
You still need a very basic form of connection pooling even if you're a single FastCGI (or other persistent) instance. Otherwise if you don't get a request for several hours the MySQL connection will expire and give you an exception when you try to use it. Any usual data access layer would give you connection pooling for free, but if you're using raw DB-API you'll probably want a little bit of manual pooling.
bobince
That's a relevant and good point.
Tor Valamo
A: 

Note: Persistent connections can have a very negative effect on your system performance. If you have a large number of web server processes all holding persistent connections to your DB server you may exhaust the DB server's limit on connections. This is one of those areas where you need to test it under heavy simulated loads to make sure you won't hit the wall at 100MPH.

Peter Rowell