G
G
Gram Orsinium2016-09-18 08:53:51
MySQL
Gram Orsinium, 2016-09-18 08:53:51

How to work with peewee from multiple threads?

Good day.
There is a multi-threaded application using peewee from several threads. Some time after launch, the following exception is thrown:

peewee.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")

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

database_locker.acquire(blocking=True)
pass #работа с БД
database_locker.release()

That's just in the case of peewee for some reason it does not work.
What do you think? Should threading.Lock help, and are there other approaches to solving the problem?
Python 3.4
PyMySQL 0.7.4
peewee 2.8.2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gram Orsinium, 2016-09-21
@orsinium

If anyone is interested, you can pass an argument to database.__init__ threadlocals=True, which seems to fix everything. As I said, the error does not appear immediately, so you have to wait a bit before you say for sure :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question