如何使用Python多线程处理MySQL连接

问题描述:

我有一个主要的Python脚本,该脚本连接到MySQL数据库并从中提取一些记录.根据返回的结果,它开始获取的记录数与线程(类实例)数相同.每个线程应返回数据库并通过将一个状态标志设置为不同的状态(进程已启动")来更新另一张表.

I have a main Python script which connects to a MySQL database and pulls out few records from it. Based on the result returned it starts as many threads (class instances) as many records are grabbed. Each thread should go back to the database and update another table by setting one status flag to a different state ("process started").

为此,我尝试:

1.)将数据库连接传递给所有线程 2.)从每个线程打开一个新的数据库连接

1.) Pass the database connection to all threads 2.) Open a new database connection from each thread

但是他们都没有工作.

but none of them were working.

在两种情况下,我都可以使用try/except来运行更新,而不会出现任何问题,但是MySQL表尚未更新,并且未生成任何错误.我在两种情况下都使用了提交.

I could run my update without any issue in both cases by using try/except, but the MySQL table has not been updated, and no error was generated. I used commit in both cases.

我的问题是在这种情况下如何处理MySQL连接?

My question would be how to handle MySQL connection(s) in such a case?

根据前几条评论进行更新:

MAIN SCRIPT
-----------

#Connecting to DB
db = MySQLdb.connect(host = db_host,
                         db = db_db,
                         port = db_port,
                         user = db_user,
                         passwd = db_password,
                         charset='utf8')

# Initiating database cursor
cur = db.cursor()

# Fetching records for which I need to initiate a class instance

cur.execute('SELECT ...')

for row in cur.fetchall() :
    # Initiating new instance, appending it to a list and
    # starting all of them 



CLASS WHICH IS INSTANTIATED
---------------------------

# Connecting to DB again. I also tried to pass connection
# which has been opened in the main script but it did not
# work either.

db = MySQLdb.connect(host = db_host,
                         db = db_db,
                         port = db_port,
                         user = db_user,
                         passwd = db_password,
                         charset='utf8')

# Initiating database cursor
cur_class = db.cursor()
cur.execute('UPDATE ...')
db.commit()

似乎我的代码没有问题,但MySQL版本没有问题.我使用的是MySQL标准社区版,并且基于的官方文档

It seems there's no problem with my code but with my MySQL version. I'm using MySQL standard community edition and based on the official documentation found here :

线程池插件是一项商业功能.它不包含在MySQL社区发行版中.

The thread pool plugin is a commercial feature. It is not included in MySQL community distributions.

我将升级到MariaDB以解决此问题.

I'm about to upgrade to MariaDB to solve this issue.