How to create a multithreaded application on Flask + SQLAlchemy?

Doing my first large application in Python: Flask + SQL Alchemy
The database is MySQL.

The point is that inside the app, you can create jobs. Each job runs in a multithreaded mode.
Inside each thread there is a lot of readings and record in database.

The problem is that for a sufficiently large number of thread (50+) MySQL just dies.
The important point is that each task runs in a separate process. Inside the process has already created threads.
I can't figure out exactly where the bottleneck is. Are simple operations to read / write data. The data volumes are not large.
Guided by its past extensive experience in PHP, I know that MySQL should be easy to withstand such loads.

Configure SQL Alchemy:
SQLALCHEMY_TRACK_MODIFICATIONS = True
SQLALCHEMY_POOL_SIZE = 20
SQLALCHEMY_MAX_OVERFLOW = 5
SQLALCHEMY_POOL_RECYCLE = 5


Create the db object:
db = SQLAlchemy(app)

Then I import and use in other scripts.

It seems to me that not correctly I do in some places.
For example, I need in real time to make the increment value in the database. This is the only working version of the code:
db.session.commit()
task = Task.query.filter_by(id=self.task.id).first()
db.session.query(Task).filter_by(id=self.task.id).update({'progress': task.progress + 1})
db.session.commit()


Key question: how correctly to organize work with a similar application on SQL Alchemy?
How to use session? And is it possible to track the time source and create a large load on MySQL?
The server is extremely powerful and is not a bottleneck. Can be specific MySQL configuration?
July 4th 19 at 22:54
0 answer

Find more questions by tags FlaskMySQL