使用 Python3 多处理时,正确的 peewee 设置可以避免 mysql 同步问题?

Proper peewee setup for avoiding mysql sync issues when using Python3 multiprocessing?

今天我接手了一个朋友的项目,这让我大吃一惊。当试图写入远程 MySQL 数据库时,它吐出了这个错误:

peewee.OperationalError: (2014, 'Command Out of Sync')

仔细观察,该程序仅在初始化阶段对数据库运行 connect() ,并且仅在程序退出时才运行 close() 。但是,这是一个多处理 Python 应用程序,它通常一次执行大约 4 个进程,每个进程都读取和写入我们的数据库。

根据我以前使用 MySQL 的经验,以及对这个特定错误的研究,这似乎是在两个语句同时执行时发生的。因此,有必要对方法进行一些重写。

大多数方法都包含在 Class 它们在数据库中交互的方法中,例如:

Class Foo

...

def register_foo(self) -> Foo:

                saved = False
                attempts = 0


                while not saved:

                        if attempts >= 20:

                                print('error: could not register foo!')
                                return None

                        try:
                                foo, created = Foo.get_or_create()

                        except Exception as e:

                                print(e)
                                sleep(2.5)
                                attempts += 1

                                continue

                        saved = True

                return foo

这似乎完全不对。我想它有时可以解决这个问题,但我更愿意通过同步到数据库来解决这个问题。

我的问题是什么是分割 Python3 脚本的 peewee 方法的正确方法,以便在写入远程数据库时不会出现同步问题?

对于在遥远的未来找到它的任何人。如果 peewee 和 MySQL 出现 2006 或 2014 错误,请使用此 mixin class:

from playhouse.shortcuts import ReconnectMixin

class ReconnectMySQLDatabase(ReconnectMixin, MySQLDatabase):
        pass

并使用 ReconnectMySQLDatabase 而不是 MySQLDatabase 进行 peewee 交互。

On closer inspection, the program only runs connect() to the DB at the init phase, and only runs close() once the program is exiting. However, this is a multiprocessing Python app, and it is usually doing about 4 processes at a time, each reading and writing to our DB.

您需要做的是在 分叉新进程后 连接到数据库。这样,当新进程启动时,内部状态(文件描述符等)是干净的。