PyMySQL 与 Django,多线程应用程序

PyMySQL with Django, Multithreaded application

我们正在尝试在我们的 Django (==1.11.4) 环境中使用 PyMySQL (==0.7.11)。但是当同时执行多个操作时我们会遇到问题(多个请求发送到同一个 API 函数)。

我们收到此错误:

pymysql.err.InternalError: Packet sequence number wrong - got 6 expected 1

我们正在尝试从数据库中删除记录(有时来自多个用户的大量请求)。

代码:

def delete(self, delete_query):
    self.try_reconnect()
    return self._execute_query(delete_query)

def try_reconnect(self):
    if not self.is_connected:
        self.connection.ping(reconnect=True)

@property
def is_connected(self)
    try:
        self.connection.ping(reconnect=False)
        return True
    execpt:
        return False

def _execute_query(self, query):
    with self.connection.cursor() as cursor:
        cursor.execute(query)
        self.connection.commit()
        last_row_id = cursor.lastrowid
    return last_row_id

我认为没有必要指出那些函数是 DBHandler 的一部分 class, self.connection 在 def connect(self) 函数中初始化。

def connect(self):
    self.connection = pymysql.connect(...)

这个连接函数 运行 在 Django 启动时,我们为整个项目创建了一个 DBHandler 的全局实例(varaible),并导入了多个文件。

我们正在使用 delete 函数作为执行删除查询的网关。

我们做错了什么?我们该如何解决?

发现问题,

PyMySQL not thread safty 像我们一样共享连接(我们在多个文件之间共享 class 实例作为全局实例 - 在 class 中只有一个连接),它被标记为 1:

threadsafety = 1

根据PEP 249

1 - Threads may share the module, but not connections.

PyMySQLgithub问题中的评论之一:

you need one pysql.connect() for each process/thread. As far as I know that's the only way to fix it. PyMySQL is not thread safe, so the same connection can't be used across multiple threads.

如果您正在考虑为您的线程应用程序使用其他 python 名为 MySQLdb 的包,请注意 MySQLdb 消息:

Don't share connections between threads. It's really not worth your effort or mine, and in the end, will probably hurt performance, since the MySQL server runs a separate thread for each connection. You can certainly do things like cache connections in a pool, and give those connections to one thread at a time. If you let two threads use a connection simultaneously, the MySQL client library will probably upchuck and die. You have been warned. For threaded applications, try using a connection pool. This can be done using the Pool module.

最终我们设法使用了 Django ORM,并且我们只为我们特定的 table 编写,通过使用 inspectdb.

进行管理