我对python还很陌生,我正在使用pythonSocketServer.ForkingCPServer创建需要连接到数据库(mysql)的网络脚本。我预计这个程序会以每秒30-40次的速度被击中。是否可以跨进程共享相同的数据库连接?在
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..."
是的,有可能。例如,http://mysql-python.sourceforge.net/MySQLdb.html是/可以是线程安全的。您可以在启动fork之前连接,然后使用全局变量。这不漂亮,但会有用的。在
您可以将连接作为参数传递给构造函数。 如果你粘贴一些有问题的代码会更好。在
相关问题 更多 >
编程相关推荐