Answer the question
In order to leave comments, you need to log in
How to avoid database is locked error?
There is a tg bot on aiogram in a combination of aiosqlite and selenium, ran into a problem when scaling
When working with more than 20 browsers, it starts spamming the database is locked exception,
which tightly puts the bot itself, and starts skipping tasks.
I localized the problem, but I can’t figure out how to solve it, in the bottom line there are about 20 threads that access the database about 2-3 times per second.
Here is the update code:
async def update_balance.(rate: float, account_id: int):
try:
db = await aiosqlite.connect('ozon.db')
await db.execute('''
UPDATE balance, accounts
SET balance.user_balance - {0}, accounts.cards + 1
WHERE accounts.account_id = {1}
AND balance.user_id = accoounts.user_id'''.format(2*rate, account_id)
)
except aiosqlite.DatabaseError as e:
logging.error('Ошибка при записи баланса', e)
await db.rollback()
finally:
await db.close()
Answer the question
In order to leave comments, you need to log in
Sqlite is designed like this - there can be many reading connections, but only one writing connection at a time. Moreover, by default, the writing connection will block all readers.
In order for readers not to be blocked, you need to enable WAL
. In this case, the writing connection will still be one at a time. If this does not suit you, you have chosen the wrong tool, and you should look towards normal server DBMS (mysql, postgresql).
Theoretically, setting a longer timeout could help (but I don't see how to change it at all in aiosqlite) or catching the "database is locked" error and trying again.
db.execute("PRAGMA journal_mode=WAL")
How to avoid database is locked error?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question