有没有办法在 python 应用程序 运行 时连接 mysql?

Is there a way to connect mysql while a python application running?

我想创建一个 python 应用程序并从 mysql 服务器检索查询结果。

因此,每次发生事件时,应用程序都会通过 mysql 客户端连接到 mysql,如下所示

# retrieve query result from db
db=_mysql.connect(host="localhost",user="root",
                  passwd="password",db="db")

有没有办法不每次都尝试连接而是保持连接?

改变

begin loop:
    get MQ msg
    db=_mysql.connect(host="localhost",user="root",
                  passwd="password",db="db")
    db.insert()

db=_mysql.connect(host="localhost",user="root",
                  passwd="password",db="db")
begin loop:
    get MQ msg
    db.insert()