简体   繁体   English

Python中的持久MySQL连接

[英]Persistent MySQL connections in Python

PHP provides mysql_connect() and mysql_pconnect() which allow creating both temporary and persistent database connections. PHP提供了mysql_connect()和mysql_pconnect(),它们允许创建临时和持久数据库连接。

Is there a similar functionality in Python? Python中是否有类似的功能? The environment on which this will be used is lighttpd server with FastCGI. 将使用该环境的是带有FastCGI的lighttpd服务器。

Thank you! 谢谢!

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. 如果您使用的是FastCGI,则不需要“持久连接”,因为如果您创建连接,则默认情况下它是持久的,因为FastCGI python不是基于请求的,而是不断运行的。

This is how FastCGI works in python, to put it short: 简而言之,这就是FastCGI在python中的工作方式:

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. 在PHP / FastCGI中,这是不同的,因为始终只加载PHP引擎,而针对每个请求执行PHP脚本本身。

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. 因此区别在于,在Python中,您可以定义自己的初始化。 And that's where you put your MySQL connection. 这就是放置MySQL连接的地方。 :) :)

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. 如果您有大量的Web服务器进程都与数据库服务器建立了永久连接,则可能会耗尽数据库服务器的连接限制。 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. 这是您需要在重的模拟负载下进行测试以确保在100MPH时不会撞墙的区域之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM