What is the best way to manage db connection in a preforking daemon

I am pretty new to python and am using pythons SocketServer.ForkingTCPServer to create a network script that needs to connect to a database(mysql). I expect the program to get hit at around 30 - 40 times a second. Is it possible to share the same database connection across the processes?

import os
import SocketServer
import MySQLdb

class EchoHandler(SocketServer.StreamRequestHandler):
    def handle(self):
                self.wfile.write("SET VARIABLE DBDIALSTRING dbstuff n")
                self.wfile.flush()
                self.conn.close()

if __name__ == '__main__':


    conn = MySQLdb.connect (host = "10.0.0.12", user = "dbuser", passwd = "secert", db = "dbname")
    SocketServer.ForkingTCPServer.allow_reuse_address = 1
    server = SocketServer.ForkingTCPServer(('10.0.0.10', 4242), EchoHandler)
    print "Server listening on localhost:4242..."
    try:
        server.allow_reuse_address
        server.serve_forever()
    except KeyboardInterrupt:
        print "nbailing..."

Yes, it's possible. For example this http://mysql-python.sourceforge.net/MySQLdb.html is/can be thread safe. You can connect before starting forks and than use global variable. It's not pretty but will work.

You can pass connection as a parameter to constructor. It will be better if you paste some code in question.

链接地址: http://www.djcxy.com/p/56450.html

上一篇: 释放动作到一个kivy按钮

下一篇: 在preforking守护进程中管理数据库连接的最佳方法是什么?