How to work with peewee from multiple threads?

Good time of day.

There is a multithreaded application that uses multiple threads to peewee. Shortly after launch, crashes with the following exception:
peewee.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")

In fact, this is, of course, falls off PyMySQL, which uses the kernel peewee. Based on experience, to work with PyMySQL helped threading.Lock:
from threading import Lock
database_locker = Lock()

database_locker.acquire(blocking=True)
pass #work with DB
database_locker.release()

That's only if peewee is somehow not working.
What do you think? Should help threading.Lock, and whether there are other approaches to solving the problem?

Python 3.4
PyMySQL 0.7.4
peewee 2.8.2
July 4th 19 at 23:29
1 answer
July 4th 19 at 23:31
Solution
If anyone is interested, then in the database.__init__ you can pass an argument threadlocals=True, which sort of fixes things. As I said, the error is not immediately, so we'll have to wait a bit before saying for sure :)

Find more questions by tags multithreadingMySQLPython