{"id":500429,"date":"2026-10-27T10:08:29","date_gmt":"2026-10-27T07:08:29","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=500429"},"modified":"2026-10-27T10:08:29","modified_gmt":"2026-10-27T07:08:29","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=500429","title":{"rendered":"database is locked. \u0427\u0442\u043e \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 SQLite \u0432 Telegram-\u0431\u043e\u0442\u0435 \u0438 \u043a\u0430\u043a \u044f \u044d\u0442\u043e \u0447\u0438\u043d\u044e"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0411\u043e\u0442 \u0434\u043b\u044f \u043f\u0440\u0438\u0451\u043c\u0430 \u0437\u0430\u043a\u0430\u0437\u043e\u0432, aiogram, \u0431\u0430\u0437\u0430 SQLite \u0432 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435. \u0417\u0430\u043a\u0430\u0437\u0447\u0438\u043a\u0443 \u043d\u0443\u0436\u043d\u0430 \u0431\u044b\u043b\u0430 \u0435\u0449\u0451 \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u0430\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0432 Excel, \u0438 \u0435\u0451 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c \u043f\u043e \u043a\u0440\u043e\u043d\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0447\u0438\u0442\u0430\u0435\u0442 \u0442\u043e\u0442 \u0436\u0435 \u0444\u0430\u0439\u043b. \u041c\u0435\u0441\u044f\u0446 \u0432\u0441\u0451 \u0436\u0438\u043b\u043e \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e. \u041f\u043e\u0442\u043e\u043c \u0437\u0430\u043a\u0430\u0437\u0447\u0438\u043a \u043f\u0440\u0438\u0441\u043b\u0430\u043b \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442: \u0431\u043e\u0442 \u043d\u0430 \u00ab\u041e\u0444\u043e\u0440\u043c\u0438\u0442\u044c \u0437\u0430\u043a\u0430\u0437\u00bb \u043c\u043e\u043b\u0447\u0438\u0442 \u0441\u0435\u043a\u0443\u043d\u0434 \u043f\u044f\u0442\u044c, \u0430 \u043f\u043e\u0442\u043e\u043c \u043f\u0438\u0448\u0435\u0442 \u00ab\u0427\u0442\u043e-\u0442\u043e \u043f\u043e\u0448\u043b\u043e \u043d\u0435 \u0442\u0430\u043a\u00bb. \u0412 \u043b\u043e\u0433\u0430\u0445:<\/p>\n<pre><code>sqlite3.OperationalError: database is locked<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper\"><\/span><\/a><\/div><\/pre>\n<p>\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0430\u044f, \u0440\u0430\u0437 \u0432 \u0434\u0432\u0430-\u0442\u0440\u0438 \u0434\u043d\u044f, \u0430 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0442\u0430\u043c \u0434\u0435\u0441\u044f\u0442\u043e\u043a \u0437\u0430\u043a\u0430\u0437\u043e\u0432 \u0432 \u0447\u0430\u0441. \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u044f \u043f\u043e\u0442\u043e\u043c \u043d\u0430\u0448\u0451\u043b \u0441\u0440\u0430\u0437\u0443 \u0434\u0432\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0438\u0437 \u0442\u0440\u0451\u0445, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0438\u0436\u0435. \u0422\u0440\u0435\u0442\u044c\u044e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b \u0432 \u0447\u0443\u0436\u0438\u0445 \u0431\u043e\u0442\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043d\u043e\u0441\u0438\u043b\u0438 \u043d\u0430 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0443.<\/p>\n<h3>\u041a\u0430\u043a SQLite \u0432\u043e\u043e\u0431\u0449\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442<\/h3>\n<p>\u0412 \u0440\u0435\u0436\u0438\u043c\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e (rollback journal) \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0431\u0430\u0437\u0443 \u044d\u043a\u0441\u043a\u043b\u044e\u0437\u0438\u0432\u043d\u0430\u044f. \u041f\u043e\u043a\u0430 \u043e\u0434\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0442\u043a\u0440\u044b\u0442\u0443\u044e \u043f\u0438\u0448\u0443\u0449\u0443\u044e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e, \u0434\u0440\u0443\u0433\u043e\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0447\u0430\u0442\u044c \u0441\u0432\u043e\u044e. \u0412\u0442\u043e\u0440\u043e\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 SQLITE_BUSY, \u0438 \u043c\u043e\u0434\u0443\u043b\u044c <code>sqlite3<\/code> \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 <code>database is locked<\/code>.<\/p>\n<p>\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u0441\u0434\u0430\u0442\u044c\u0441\u044f, <code>sqlite3<\/code> \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0436\u0434\u0451\u0442. \u0417\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>timeout<\/code> \u0443 <code>connect()<\/code>, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 5 \u0441\u0435\u043a\u0443\u043d\u0434. \u0422\u0435 \u0441\u0430\u043c\u044b\u0435 \u043f\u044f\u0442\u044c \u0441\u0435\u043a\u0443\u043d\u0434 \u0442\u0438\u0448\u0438\u043d\u044b \u0432 \u0431\u043e\u0442\u0435.<\/p>\n<p>\u0412\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a:<\/p>\n<pre><code class=\"python\">import sqlite3, timedb = sqlite3.connect(\"shop.db\")db.execute(\"CREATE TABLE IF NOT EXISTS orders(\"           \"id INTEGER PRIMARY KEY, user_id INT, status TEXT)\")db.commit()a = sqlite3.connect(\"shop.db\")b = sqlite3.connect(\"shop.db\", timeout=1)a.execute(\"INSERT INTO orders(user_id, status) VALUES (1, 'new')\")print(a.in_transaction)  # Truet = time.perf_counter()try:    b.execute(\"INSERT INTO orders(user_id, status) VALUES (2, 'new')\")except sqlite3.OperationalError as e:    print(e, round(time.perf_counter() - t, 2))# database is locked 1.15<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 <a href=\"http:\/\/a.in\" rel=\"noopener nofollow\"><code>a.in<\/code><\/a><code>_transaction<\/code>. \u042f \u043d\u0438\u0433\u0434\u0435 \u043d\u0435 \u043f\u0438\u0441\u0430\u043b <code>BEGIN<\/code>, \u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0430.<\/p>\n<h3>\u041f\u0440\u0438\u0447\u0438\u043d\u0430 1. \u041d\u0435\u044f\u0432\u043d\u0430\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0438 \u0437\u0430\u0431\u044b\u0442\u044b\u0439 commit<\/h3>\n<p>\u041c\u043e\u0434\u0443\u043b\u044c <code>sqlite3<\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u00ablegacy\u00bb-\u0440\u0435\u0436\u0438\u043c\u0435: \u043f\u0435\u0440\u0435\u0434 \u043f\u0435\u0440\u0432\u044b\u043c INSERT, UPDATE \u0438\u043b\u0438 DELETE \u043e\u043d \u0441\u0430\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 <code>BEGIN<\/code>, \u0430 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 <code>commit()<\/code> \u0438\u043b\u0438 <code>rollback()<\/code>. \u0415\u0441\u043b\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u043a\u043e\u0434 \u0443\u0448\u0451\u043b \u0436\u0434\u0430\u0442\u044c \u0447\u0435\u0433\u043e-\u0442\u043e \u0434\u043e\u043b\u0433\u043e\u0433\u043e \u0438\u043b\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u0437\u0430\u0431\u044b\u043b \u0437\u0430\u043a\u043e\u043c\u043c\u0438\u0442\u0438\u0442\u044c, \u0444\u0430\u0439\u043b \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445.<\/p>\n<p>\u0412 \u0442\u043e\u043c \u0431\u043e\u0442\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">@router.message(OrderForm.address)async def got_address(message: Message, state: FSMContext):    cur.execute(\"UPDATE users SET address = ? WHERE tg_id = ?\",                (message.text, message.from_user.id))    data = await state.get_data()    await message.answer(render_summary(data))  # \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a Telegram    conn.commit()<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>UPDATE \u043e\u0442\u043a\u0440\u044b\u043b \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e, \u0434\u0430\u043b\u044c\u0448\u0435 \u0445\u0435\u043d\u0434\u043b\u0435\u0440 \u043f\u043e\u0448\u0451\u043b \u0432 \u0441\u0435\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e \u0434\u043e\u043b\u0438 \u0441\u0435\u043a\u0443\u043d\u0434\u044b. \u041d\u043e \u0435\u0441\u043b\u0438 Telegram \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0438\u043b\u0438 \u0431\u043e\u0442 \u0443\u043f\u0451\u0440\u0441\u044f \u0432\u043e \u0444\u043b\u0443\u0434-\u043b\u0438\u043c\u0438\u0442, \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0432\u0438\u0441\u0438\u0442, \u0430 \u043a\u0440\u043e\u043d \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0438\u0448\u0435\u0442 \u0432 \u0442\u0443 \u0436\u0435 \u0431\u0430\u0437\u0443 \u043e\u0442\u043c\u0435\u0442\u043a\u0443 \u043e \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0435. \u0425\u0443\u0436\u0435, \u0435\u0441\u043b\u0438 \u043c\u0435\u0436\u0434\u0443 UPDATE \u0438 <code>commit()<\/code> \u0432\u044b\u043b\u0435\u0442\u0435\u043b\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435: aiogram \u0435\u0433\u043e \u0437\u0430\u043b\u043e\u0433\u0438\u0440\u0443\u0435\u0442, \u0445\u0435\u043d\u0434\u043b\u0435\u0440 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f, \u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043d\u0430 \u044d\u0442\u043e\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438 \u0442\u0430\u043a \u0438 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e <code>commit()<\/code> \u0433\u0434\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043c\u0435\u0441\u0442\u0435.<\/p>\n<p>\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043a\u0443\u0447\u043d\u043e\u0435: \u043a\u043e\u043c\u043c\u0438\u0442 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u0434\u043e \u043b\u044e\u0431\u044b\u0445 <code>await<\/code>. \u0423\u0434\u043e\u0431\u043d\u0435\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>with conn:<\/code>, \u043e\u043d \u0434\u0435\u043b\u0430\u0435\u0442 commit \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0445\u0435 \u0438 rollback \u043f\u0440\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438.<\/p>\n<h3>\u041f\u0440\u0438\u0447\u0438\u043d\u0430 2. \u00ab\u041f\u0440\u043e\u0447\u0438\u0442\u0430\u043b, \u043f\u043e\u0442\u043e\u043c \u0437\u0430\u043f\u0438\u0441\u0430\u043b\u00bb<\/h3>\n<p>\u042d\u0442\u0443 \u044f \u043b\u044e\u0431\u043b\u044e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0441\u0435\u0433\u043e: <code>timeout<\/code> \u0442\u0443\u0442 \u043d\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442, \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u0435\u0442 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e.<\/p>\n<p>\u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043a\u043e\u0434: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432 \u0431\u0430\u0437\u0435 \u0438 \u043f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0443 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0440\u0451\u0445 \u043d\u0435\u0437\u0430\u043a\u0440\u044b\u0442\u044b\u0445 \u0437\u0430\u043a\u0430\u0437\u043e\u0432 \u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<pre><code class=\"python\">a = sqlite3.connect(\"shop.db\", timeout=5, isolation_level=None)b = sqlite3.connect(\"shop.db\", timeout=5, isolation_level=None)a.execute(\"PRAGMA journal_mode=WAL\")a.execute(\"BEGIN\")n = a.execute(\"SELECT count(*) FROM orders \"              \"WHERE user_id = 1 AND status = 'new'\").fetchone()[0]# \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0434\u0440\u0443\u0433\u043e\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0443\u0441\u043f\u0435\u043b\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044cb.execute(\"INSERT INTO orders(user_id, status) VALUES (3, 'new')\")try:    a.execute(\"INSERT INTO orders(user_id, status) VALUES (1, 'new')\")except sqlite3.OperationalError as e:    print(e, e.sqlite_errorname)# database is locked SQLITE_BUSY_SNAPSHOT<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u0412\u0440\u0435\u043c\u044f \u0434\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0443 \u043c\u0435\u043d\u044f 0.0 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u0445\u043e\u0442\u044f <code>timeout=5<\/code>. \u041e\u0431\u044b\u0447\u043d\u044b\u0439 <code>BEGIN<\/code> \u0432 SQLite \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 <code>BEGIN DEFERRED<\/code>: \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u043a\u0430\u043a \u0447\u0438\u0442\u0430\u044e\u0449\u0430\u044f \u0438 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0442\u044c \u043f\u0438\u0448\u0443\u0449\u0435\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c INSERT. \u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 <code>a<\/code> \u0443\u0436\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u043e \u0441\u043d\u0438\u043c\u043e\u043a \u0431\u0430\u0437\u044b, \u0430 <code>b<\/code> \u0443\u0441\u043f\u0435\u043b\u043e \u0435\u0451 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c. \u0414\u0430\u0442\u044c <code>a<\/code> \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u0432\u0435\u0440\u0445 \u0441\u0442\u0430\u0440\u043e\u0433\u043e \u0441\u043d\u0438\u043c\u043a\u0430 SQLite \u043d\u0435 \u043c\u043e\u0436\u0435\u0442, \u0438 \u0436\u0434\u0430\u0442\u044c \u0442\u0443\u0442 \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e, \u0441\u043d\u0438\u043c\u043e\u043a \u043e\u0442 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0441\u0432\u0435\u0436\u0435\u0435 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 busy handler \u0434\u0430\u0436\u0435 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0438\u0441\u0430\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u0435\u0451 \u043f\u0438\u0448\u0443\u0449\u0435\u0439.<\/p>\n<pre><code class=\"python\">a.execute(\"BEGIN IMMEDIATE\")n = a.execute(\"SELECT count(*) FROM orders \"              \"WHERE user_id = 1 AND status = 'new'\").fetchone()[0]if n &lt; 3:    a.execute(\"INSERT INTO orders(user_id, status) VALUES (1, 'new')\")a.execute(\"COMMIT\")<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p><code>BEGIN IMMEDIATE<\/code> \u0431\u0435\u0440\u0451\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043d\u0430\u0447\u0430\u043b\u0435. \u0415\u0441\u043b\u0438 \u0435\u0451 \u0434\u0435\u0440\u0436\u0438\u0442 \u043a\u0442\u043e-\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0439, \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0447\u0435\u0441\u0442\u043d\u043e \u0436\u0434\u0451\u0442 <code>timeout<\/code>, \u0430 \u043d\u0435 \u043f\u0430\u0434\u0430\u0435\u0442. \u042f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u043b \u043d\u0430 \u0434\u0432\u0443\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445: \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0430 \u0441\u0432\u043e\u0451\u043c <code>BEGIN IMMEDIATE<\/code> \u043f\u043e\u0434\u043e\u0436\u0434\u0430\u043b 0.52 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u043f\u043e\u043a\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442, \u0438 \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u043b. \u0417\u0430\u043e\u0434\u043d\u043e \u043f\u0440\u043e\u043f\u0430\u043b\u0430 \u0433\u043e\u043d\u043a\u0430, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0431\u0430 \u0432\u0438\u0434\u0435\u043b\u0438 \u00ab\u0434\u0432\u0430 \u0437\u0430\u043a\u0430\u0437\u0430\u00bb \u0438 \u043e\u0431\u0430 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438 \u0442\u0440\u0435\u0442\u0438\u0439.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435\u0441\u044c \u043d\u0435\u044f\u0432\u043d\u044b\u043c\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u043c\u043e\u0434\u0443\u043b\u044f, \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c <code>isolation_level=\"IMMEDIATE\"<\/code> \u0432 <code>connect()<\/code>, \u0442\u043e\u0433\u0434\u0430 \u043c\u043e\u0434\u0443\u043b\u044c \u0441\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c <code>BEGIN IMMEDIATE<\/code> \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e <code>BEGIN<\/code>. \u041d\u043e \u043d\u0435\u044f\u0432\u043d\u044b\u0439 BEGIN \u0441\u0442\u0430\u0432\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u0434 DML, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 SELECT \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u00ab\u043f\u0440\u043e\u0432\u0435\u0440\u044c \u0438 \u0437\u0430\u043f\u0438\u0448\u0438\u00bb \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432\u043d\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u042f \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u044e <code>isolation_level=None<\/code> \u0438 \u044f\u0432\u043d\u044b\u0439 <code>BEGIN IMMEDIATE<\/code>: \u0442\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0432 \u043a\u043e\u0434\u0435, \u0433\u0434\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<h3>\u041f\u0440\u0438\u0447\u0438\u043d\u0430 3. \u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 sqlite3 \u0432\u043d\u0443\u0442\u0440\u0438 asyncio<\/h3>\n<p>\u0421\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u0440\u0430\u0439\u0432\u0435\u0440 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442, \u0437\u0430\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0451 \u0432 \u0440\u0430\u0437\u044b \u0431\u043e\u043b\u044c\u043d\u0435\u0435. \u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 <code>execute()<\/code> \u0436\u0434\u0451\u0442 \u0441\u0432\u043e\u0438 5 \u0441\u0435\u043a\u0443\u043d\u0434 \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u0446\u0438\u043a\u043b\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0412\u0435\u0441\u044c \u0431\u043e\u0442 \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u0442\u043e\u0438\u0442: \u043d\u0435 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c, \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043b\u0431\u044d\u043a\u0438, \u043d\u0435 \u0448\u043b\u0451\u0442 \u043d\u0438\u0447\u0435\u0433\u043e.<\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430:<\/p>\n<pre><code class=\"python\">import asyncio, sqlite3, threading, timedef hold_lock():    c = sqlite3.connect(\"shop.db\")    c.execute(\"INSERT INTO orders(user_id, status) VALUES (7, 'new')\")    time.sleep(3)    c.commit()async def ticker():    for _ in range(6):        print(time.strftime(\"%X\"), \"\u0442\u0438\u043a\")        await asyncio.sleep(0.5)async def handler():    await asyncio.sleep(0.2)    conn = sqlite3.connect(\"shop.db\", timeout=5)    conn.execute(\"INSERT INTO orders(user_id, status) VALUES (8, 'new')\")    conn.commit()    print(time.strftime(\"%X\"), \"\u0437\u0430\u043f\u0438\u0441\u0430\u043b\")async def main():    threading.Thread(target=hold_lock).start()    await asyncio.gather(ticker(), handler())asyncio.run(main())<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434:<\/p>\n<pre><code>09:47:07 \u0442\u0438\u043a09:47:10 \u0437\u0430\u043f\u0438\u0441\u0430\u043b09:47:10 \u0442\u0438\u043a09:47:10 \u0442\u0438\u043a09:47:11 \u0442\u0438\u043a...<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u041c\u0435\u0436\u0434\u0443 \u043f\u0435\u0440\u0432\u044b\u043c \u0438 \u0432\u0442\u043e\u0440\u044b\u043c \u0442\u0438\u043a\u043e\u043c \u0442\u0440\u0438 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u044b. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0431\u043e\u0442\u0435 \u044d\u0442\u0438 \u0442\u0440\u0438 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0432\u0441\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0440\u0430\u0437\u043e\u043c, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0442, \u043a\u0442\u043e \u043d\u0430\u0436\u0430\u043b \u043a\u043d\u043e\u043f\u043a\u0443.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u0440\u0430\u0439\u0432\u0435\u0440. <code>aiosqlite<\/code> \u0433\u043e\u043d\u044f\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0446\u0438\u043a\u043b\u0443, \u043f\u043e\u043a\u0430 \u0436\u0434\u0451\u0442. \u0424\u0430\u0439\u043b \u043e\u043d \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442, \u043d\u043e \u0431\u043e\u0442 \u0445\u043e\u0442\u044f \u0431\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c.<\/p>\n<h3>\u0427\u0442\u043e \u044f \u0441\u0442\u0430\u0432\u043b\u044e \u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u043e\u0442 \u043d\u0430 SQLite<\/h3>\n<p>\u0421\u043e\u0431\u0440\u0430\u043b \u0432\u0441\u0451 \u0432 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043b\u0430\u0441\u0441. \u041e\u0434\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, WAL, <code>busy_timeout<\/code>, \u044f\u0432\u043d\u044b\u0435 \u043f\u0438\u0448\u0443\u0449\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.<\/p>\n<pre><code class=\"python\">import asynciofrom contextlib import asynccontextmanagerimport aiosqliteclass Database:    def __init__(self, path: str):        self.path = path        self.conn: aiosqlite.Connection | None = None        self._write_lock = asyncio.Lock()    async def connect(self):        # isolation_level=None: \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0430\u043c\u0438        self.conn = await aiosqlite.connect(self.path, isolation_level=None)        await self.conn.execute(\"PRAGMA journal_mode=WAL\")        await self.conn.execute(\"PRAGMA synchronous=NORMAL\")        await self.conn.execute(\"PRAGMA busy_timeout=5000\")        await self.conn.execute(\"PRAGMA foreign_keys=ON\")    async def close(self):        await self.conn.close()    @asynccontextmanager    async def write(self):        async with self._write_lock:            await self.conn.execute(\"BEGIN IMMEDIATE\")            try:                yield self.conn            except BaseException:                await self.conn.execute(\"ROLLBACK\")                raise            else:                await self.conn.execute(\"COMMIT\")    async def fetchone(self, sql, params=()):        async with self.conn.execute(sql, params) as cur:            return await cur.fetchone()<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0435:<\/p>\n<pre><code class=\"python\">async def create_order(db: Database, user_id: int) -&gt; bool:    async with db.write() as conn:        row = await db.fetchone(            \"SELECT count(*) FROM orders WHERE user_id = ? AND status = 'new'\",            (user_id,),        )        if row[0] &gt;= 3:            return False        await conn.execute(            \"INSERT INTO orders(user_id, status) VALUES (?, 'new')\", (user_id,)        )        return True<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><span class=\"sc-logo-wrapper icon-only\"><\/span><\/a><\/div><\/pre>\n<p>\u041f\u0440\u043e\u0433\u043d\u0430\u043b 500 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 <code>create_order<\/code> \u0447\u0435\u0440\u0435\u0437 <code>asyncio.gather<\/code> \u043d\u0430 10 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. \u0412 \u0431\u0430\u0437\u0435 \u0440\u043e\u0432\u043d\u043e 30 \u0437\u0430\u043a\u0430\u0437\u043e\u0432, \u043f\u043e \u0442\u0440\u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e, \u043d\u0438 \u043e\u0434\u043d\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438. \u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 <code>write()<\/code> \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e, \u044d\u0442\u043e \u0442\u043e\u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043b.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u0447\u0435\u043c \u0442\u0430\u043c \u043a\u0430\u0436\u0434\u0430\u044f \u0441\u0442\u0440\u043e\u0447\u043a\u0430.<\/p>\n<p>\u041f\u0440\u043e WAL. \u0412 \u0440\u0435\u0436\u0438\u043c\u0435 \u0436\u0443\u0440\u043d\u0430\u043b\u0430 \u043f\u0440\u0435\u0434\u0437\u0430\u043f\u0438\u0441\u0438 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043d\u0435 \u043c\u0435\u0448\u0430\u044e\u0442 \u043f\u0438\u0441\u0430\u0442\u0435\u043b\u044e \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442. \u041a\u0440\u043e\u043d \u0441 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u0432 Excel \u043c\u043e\u0436\u0435\u0442 \u0447\u0438\u0442\u0430\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0433\u043e\u0434\u043d\u043e, \u0431\u043e\u0442 \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0438\u0448\u0435\u0442. \u041f\u0438\u0441\u0430\u0442\u0435\u043b\u044c \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043e\u0434\u0438\u043d, \u0434\u043b\u044f \u0431\u043e\u0442\u0430 \u044d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430. \u0420\u0435\u0436\u0438\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0444\u0430\u0439\u043b\u0435 \u0431\u0430\u0437\u044b, \u043d\u043e PRAGMA \u0432 <code>connect()<\/code> \u044f \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e. \u0420\u044f\u0434\u043e\u043c \u0441 \u0431\u0430\u0437\u043e\u0439 \u043f\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u0444\u0430\u0439\u043b\u044b <code>-wal<\/code> \u0438 <code>-shm<\/code>, \u044d\u0442\u043e \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e. \u0422\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0431\u044d\u043a\u0430\u043f\u0430 \u043e\u0434\u0438\u043d <code>.db<\/code>, \u043f\u043e\u043a\u0430 \u0431\u043e\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u0435\u043b\u044c\u0437\u044f: \u0441\u0432\u0435\u0436\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u0432 <code>-wal<\/code>. \u0414\u043b\u044f \u0431\u044d\u043a\u0430\u043f\u0430 \u0435\u0441\u0442\u044c <code>conn.backup()<\/code> \u0438\u043b\u0438 <code>VACUUM INTO<\/code>. \u0418 WAL \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445, \u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e\u043c Docker volume \u0432\u0441\u0451 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435.<\/p>\n<p><code>synchronous=NORMAL<\/code> \u0432 WAL \u044d\u0442\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441: \u043f\u0440\u0438 \u043f\u0430\u0434\u0435\u043d\u0438\u0438 \u043f\u0438\u0442\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043d\u043e \u0431\u0430\u0437\u0430 \u043d\u0435 \u043f\u043e\u0431\u044c\u0451\u0442\u0441\u044f. \u0414\u043b\u044f \u0431\u043e\u0442\u0430 \u0441 \u0437\u0430\u043a\u0430\u0437\u0430\u043c\u0438 \u043c\u0435\u043d\u044f \u044d\u0442\u043e \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442. \u0415\u0441\u043b\u0438 \u0434\u043b\u044f \u0432\u0430\u0441 \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442, \u0443\u0431\u0435\u0440\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<p><code>busy_timeout<\/code> \u044d\u0442\u043e \u0442\u043e\u0442 \u0436\u0435 <code>timeout<\/code> \u0438\u0437 <code>connect()<\/code>, \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445 \u0438 \u0447\u0435\u0440\u0435\u0437 PRAGMA. \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432: \u0442\u043e\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043a\u0440\u043e\u043d\u0430 \u0438\u043b\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u0440\u0443\u043a\u0430\u043c\u0438 \u043d\u0430 \u043f\u0440\u043e\u0434\u0435.<\/p>\n<p><code>asyncio.Lock<\/code> \u043d\u0443\u0436\u0435\u043d, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e, \u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043c\u043d\u043e\u0433\u043e. \u0411\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u0432\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u043e\u043f\u044b\u0442\u0430\u044e\u0442\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u044c <code>BEGIN IMMEDIATE<\/code> \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438 \u0442\u043e\u043c \u0436\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438, \u0438 \u0432\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442 <code>cannot start a transaction within a transaction<\/code>. \u042f \u043d\u0430 \u044d\u0442\u043e \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b, \u043a\u043e\u0433\u0434\u0430 \u0441\u043e\u0431\u0438\u0440\u0430\u043b \u043f\u0440\u0438\u043c\u0435\u0440. Lock \u0432\u044b\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0430 <code>busy_timeout<\/code> \u0440\u0430\u0437\u0440\u0443\u043b\u0438\u0432\u0430\u0435\u0442 \u043a\u043e\u043d\u043a\u0443\u0440\u0435\u043d\u0446\u0438\u044e \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430\u043c\u0438.<\/p>\n<p>\u0418 \u0435\u0449\u0451 \u043f\u0440\u043e \u043e\u0434\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435: \u0447\u0442\u0435\u043d\u0438\u0435 \u0432\u043d\u0435 <code>write()<\/code> \u0438\u0434\u0451\u0442 \u0447\u0435\u0440\u0435\u0437 \u0442\u043e \u0436\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043d\u0435\u0437\u0430\u043a\u043e\u043c\u043c\u0438\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0440\u0443\u0433\u0430\u044f \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435 \u0441\u0432\u043e\u0435\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u0415\u0441\u043b\u0438 \u0434\u043b\u044f \u0432\u0430\u0441 \u044d\u0442\u043e \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e, \u0437\u0430\u0432\u0435\u0434\u0438\u0442\u0435 \u0432\u0442\u043e\u0440\u043e\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f. \u0412 \u043c\u043e\u0438\u0445 \u0431\u043e\u0442\u0430\u0445 \u0442\u0430\u043a\u043e\u0433\u043e \u043d\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c.<\/p>\n<h3>\u0427\u0435\u043c \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u043e\u0441\u044c \u0443 \u0437\u0430\u043a\u0430\u0437\u0447\u0438\u043a\u0430<\/h3>\n<p>\u0412 \u0442\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u044f \u043f\u0435\u0440\u0435\u0432\u0451\u043b \u0431\u0430\u0437\u0443 \u0432 WAL, \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043b \u0437\u0430\u043f\u0438\u0441\u044c \u0437\u0430\u043a\u0430\u0437\u0430 \u043d\u0430 <code>BEGIN IMMEDIATE<\/code>, \u0432\u044b\u043d\u0435\u0441 <code>commit()<\/code> \u0434\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0438 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u043b <code>sqlite3<\/code> \u043d\u0430 <code>aiosqlite<\/code>. \u0421 \u0442\u0435\u0445 \u043f\u043e\u0440 <code>database is locked<\/code> \u0432 \u043b\u043e\u0433\u0430\u0445 \u043d\u0435 \u043f\u043e\u044f\u0432\u043b\u044f\u043b\u0441\u044f. \u0417\u0430\u043a\u0430\u0437\u0447\u0438\u043a\u0443 \u0432 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u0447\u0430\u0442\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043e\u0432\u0435\u0442\u043e\u0432\u0430\u043b\u0438 \u043f\u0435\u0440\u0435\u0435\u0437\u0436\u0430\u0442\u044c \u043d\u0430 PostgreSQL. \u0420\u0430\u0434\u0438 \u0434\u0435\u0441\u044f\u0442\u0438 \u0437\u0430\u043a\u0430\u0437\u043e\u0432 \u0432 \u0447\u0430\u0441 \u044f \u0441\u043c\u044b\u0441\u043b\u0430 \u043d\u0435 \u0432\u0438\u0434\u0435\u043b. \u0415\u0441\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0430\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u0438\u0448\u0443\u0442 \u0432 \u043e\u0434\u043d\u0443 \u0431\u0430\u0437\u0443 \u0438\u043b\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u0434\u043d\u043e\u0433\u043e, \u0442\u043e\u0433\u0434\u0430 \u0434\u0430, \u043f\u043e\u0440\u0430.<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1099860\/\">https:\/\/habr.com\/ru\/articles\/1099860\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0411\u043e\u0442 \u0434\u043b\u044f \u043f\u0440\u0438\u0451\u043c\u0430 \u0437\u0430\u043a\u0430\u0437\u043e\u0432, aiogram, \u0431\u0430\u0437\u0430 SQLite \u0432 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435. \u0417\u0430\u043a\u0430\u0437\u0447\u0438\u043a\u0443 \u043d\u0443\u0436\u043d\u0430 \u0431\u044b\u043b\u0430 \u0435\u0449\u0451 \u0435\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u0430\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0432 Excel, \u0438 \u0435\u0451 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0441\u043a\u0440\u0438\u043f\u0442\u043e\u043c \u043f\u043e \u043a\u0440\u043e\u043d\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0447\u0438\u0442\u0430\u0435\u0442 \u0442\u043e\u0442 \u0436\u0435 \u0444\u0430\u0439\u043b. \u041c\u0435\u0441\u044f\u0446 \u0432\u0441\u0451 \u0436\u0438\u043b\u043e \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e. \u041f\u043e\u0442\u043e\u043c \u0437\u0430\u043a\u0430\u0437\u0447\u0438\u043a \u043f\u0440\u0438\u0441\u043b\u0430\u043b \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442: \u0431\u043e\u0442 \u043d\u0430 \u00ab\u041e\u0444\u043e\u0440\u043c\u0438\u0442\u044c \u0437\u0430\u043a\u0430\u0437\u00bb \u043c\u043e\u043b\u0447\u0438\u0442 \u0441\u0435\u043a\u0443\u043d\u0434 \u043f\u044f\u0442\u044c, \u0430 \u043f\u043e\u0442\u043e\u043c \u043f\u0438\u0448\u0435\u0442 \u00ab\u0427\u0442\u043e-\u0442\u043e \u043f\u043e\u0448\u043b\u043e \u043d\u0435 \u0442\u0430\u043a\u00bb. \u0412 \u043b\u043e\u0433\u0430\u0445:sqlite3.OperationalError: database is locked\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0430\u044f, \u0440\u0430\u0437 \u0432 \u0434\u0432\u0430-\u0442\u0440\u0438 \u0434\u043d\u044f, \u0430 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0442\u0430\u043c \u0434\u0435\u0441\u044f\u0442\u043e\u043a \u0437\u0430\u043a\u0430\u0437\u043e\u0432 \u0432 \u0447\u0430\u0441. \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u044f \u043f\u043e\u0442\u043e\u043c \u043d\u0430\u0448\u0451\u043b \u0441\u0440\u0430\u0437\u0443 \u0434\u0432\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0438\u0437 \u0442\u0440\u0451\u0445, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0438\u0436\u0435. \u0422\u0440\u0435\u0442\u044c\u044e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b \u0432 \u0447\u0443\u0436\u0438\u0445 \u0431\u043e\u0442\u0430\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043d\u043e\u0441\u0438\u043b\u0438 \u043d\u0430 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0443.\u041a\u0430\u043a SQLite \u0432\u043e\u043e\u0431\u0449\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442\u0412 \u0440\u0435\u0436\u0438\u043c\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e (rollback journal) \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u0431\u0430\u0437\u0443 \u044d\u043a\u0441\u043a\u043b\u044e\u0437\u0438\u0432\u043d\u0430\u044f. \u041f\u043e\u043a\u0430 \u043e\u0434\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0434\u0435\u0440\u0436\u0438\u0442 \u043e\u0442\u043a\u0440\u044b\u0442\u0443\u044e \u043f\u0438\u0448\u0443\u0449\u0443\u044e \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e, \u0434\u0440\u0443\u0433\u043e\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0447\u0430\u0442\u044c \u0441\u0432\u043e\u044e. \u0412\u0442\u043e\u0440\u043e\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 SQLITE_BUSY, \u0438 \u043c\u043e\u0434\u0443\u043b\u044c sqlite3 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0432 database is locked.\u041f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u0441\u0434\u0430\u0442\u044c\u0441\u044f, sqlite3 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0436\u0434\u0451\u0442. \u0417\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 timeout \u0443 connect(), \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e 5 \u0441\u0435\u043a\u0443\u043d\u0434. \u0422\u0435 \u0441\u0430\u043c\u044b\u0435 \u043f\u044f\u0442\u044c \u0441\u0435\u043a\u0443\u043d\u0434 \u0442\u0438\u0448\u0438\u043d\u044b \u0432 \u0431\u043e\u0442\u0435.\u0412\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a:import sqlite3, timedb = sqlite3.connect(&#171;shop.db&#187;)db.execute(&#171;CREATE TABLE IF NOT EXISTS orders(&#187;           &#171;id INTEGER PRIMARY KEY, user_id INT, status TEXT)&#187;)db.commit()a = sqlite3.connect(&#171;shop.db&#187;)b = sqlite3.connect(&#171;shop.db&#187;, timeout=1)a.execute(&#171;INSERT INTO orders(user_id, status) VALUES (1, &#8216;new&#8217;)&#187;)print(a.in_transaction)  # Truet = time.perf_counter()try:    b.execute(&#171;INSERT INTO orders(user_id, status) VALUES (2, &#8216;new&#8217;)&#187;)except sqlite3.OperationalError as e:    print(e, round(time.perf_counter() &#8212; t, 2))# database is locked 1.15\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 a.in_transaction. \u042f \u043d\u0438\u0433\u0434\u0435 \u043d\u0435 \u043f\u0438\u0441\u0430\u043b BEGIN, \u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u0430.\u041f\u0440\u0438\u0447\u0438\u043d\u0430 1. \u041d\u0435\u044f\u0432\u043d\u0430\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0438 \u0437\u0430\u0431\u044b\u0442\u044b\u0439 commit\u041c\u043e\u0434\u0443\u043b\u044c sqlite3 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u00ablegacy\u00bb-\u0440\u0435\u0436\u0438\u043c\u0435: \u043f\u0435\u0440\u0435\u0434 \u043f\u0435\u0440\u0432\u044b\u043c INSERT, UPDATE \u0438\u043b\u0438 DELETE \u043e\u043d \u0441\u0430\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 BEGIN, \u0430 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 commit() \u0438\u043b\u0438 rollback(). \u0415\u0441\u043b\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u043a\u043e\u0434 \u0443\u0448\u0451\u043b \u0436\u0434\u0430\u0442\u044c \u0447\u0435\u0433\u043e-\u0442\u043e \u0434\u043e\u043b\u0433\u043e\u0433\u043e \u0438\u043b\u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u0437\u0430\u0431\u044b\u043b \u0437\u0430\u043a\u043e\u043c\u043c\u0438\u0442\u0438\u0442\u044c, \u0444\u0430\u0439\u043b \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445.\u0412 \u0442\u043e\u043c \u0431\u043e\u0442\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:@router.message(OrderForm.address)async def got_address(message: Message, state: FSMContext):    cur.execute(&#171;UPDATE users SET address = ? WHERE tg_id = ?&#187;,                (message.text, message.from_user.id))    data = await state.get_data()    await message.answer(render_summary(data))  # \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043a Telegram    conn.commit()UPDATE \u043e\u0442\u043a\u0440\u044b\u043b \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e, \u0434\u0430\u043b\u044c\u0448\u0435 \u0445\u0435\u043d\u0434\u043b\u0435\u0440 \u043f\u043e\u0448\u0451\u043b \u0432 \u0441\u0435\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435. \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e \u0434\u043e\u043b\u0438 \u0441\u0435\u043a\u0443\u043d\u0434\u044b. \u041d\u043e \u0435\u0441\u043b\u0438 Telegram \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0438\u043b\u0438 \u0431\u043e\u0442 \u0443\u043f\u0451\u0440\u0441\u044f \u0432\u043e \u0444\u043b\u0443\u0434-\u043b\u0438\u043c\u0438\u0442, \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0432\u0438\u0441\u0438\u0442, \u0430 \u043a\u0440\u043e\u043d \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0438\u0448\u0435\u0442 \u0432 \u0442\u0443 \u0436\u0435 \u0431\u0430\u0437\u0443 \u043e\u0442\u043c\u0435\u0442\u043a\u0443 \u043e \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0435. \u0425\u0443\u0436\u0435, \u0435\u0441\u043b\u0438 \u043c\u0435\u0436\u0434\u0443 UPDATE \u0438 commit() \u0432\u044b\u043b\u0435\u0442\u0435\u043b\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435: aiogram \u0435\u0433\u043e \u0437\u0430\u043b\u043e\u0433\u0438\u0440\u0443\u0435\u0442, \u0445\u0435\u043d\u0434\u043b\u0435\u0440 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f, \u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043d\u0430 \u044d\u0442\u043e\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438 \u0442\u0430\u043a \u0438 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e commit() \u0433\u0434\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043c\u0435\u0441\u0442\u0435.\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043a\u0443\u0447\u043d\u043e\u0435: \u043a\u043e\u043c\u043c\u0438\u0442 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u0434\u043e \u043b\u044e\u0431\u044b\u0445 await. \u0423\u0434\u043e\u0431\u043d\u0435\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 with conn:, \u043e\u043d \u0434\u0435\u043b\u0430\u0435\u0442 commit \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0445\u0435 \u0438 rollback \u043f\u0440\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438.\u041f\u0440\u0438\u0447\u0438\u043d\u0430 2. \u00ab\u041f\u0440\u043e\u0447\u0438\u0442\u0430\u043b, \u043f\u043e\u0442\u043e\u043c \u0437\u0430\u043f\u0438\u0441\u0430\u043b\u00bb\u042d\u0442\u0443 \u044f \u043b\u044e\u0431\u043b\u044e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0441\u0435\u0433\u043e: timeout \u0442\u0443\u0442 \u043d\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442, \u043e\u0448\u0438\u0431\u043a\u0430 \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u0435\u0442 \u043c\u0433\u043d\u043e\u0432\u0435\u043d\u043d\u043e.\u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043a\u043e\u0434: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432 \u0431\u0430\u0437\u0435 \u0438 \u043f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0443 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0440\u0451\u0445 \u043d\u0435\u0437\u0430\u043a\u0440\u044b\u0442\u044b\u0445 \u0437\u0430\u043a\u0430\u0437\u043e\u0432 \u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.a = sqlite3.connect(&#171;shop.db&#187;, timeout=5, isolation_level=None)b = sqlite3.connect(&#171;shop.db&#187;, timeout=5, isolation_level=None)a.execute(&#171;PRAGMA journal_mode=WAL&#187;)a.execute(&#171;BEGIN&#187;)n = a.execute(&#171;SELECT count(*) FROM orders &#187;              &#171;WHERE user_id = 1 AND status = &#8216;new'&#187;).fetchone()[0]# \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0434\u0440\u0443\u0433\u043e\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0443\u0441\u043f\u0435\u043b\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044cb.execute(&#171;INSERT INTO orders(user_id, status) VALUES (3, &#8216;new&#8217;)&#187;)try:    a.execute(&#171;INSERT INTO orders(user_id, status) VALUES (1, &#8216;new&#8217;)&#187;)except sqlite3.OperationalError as e:    print(e, e.sqlite_errorname)# database is locked SQLITE_BUSY_SNAPSHOT\u0412\u0440\u0435\u043c\u044f \u0434\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0443 \u043c\u0435\u043d\u044f 0.0 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u0445\u043e\u0442\u044f timeout=5. \u041e\u0431\u044b\u0447\u043d\u044b\u0439 BEGIN \u0432 SQLite \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 BEGIN DEFERRED: \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u043a\u0430\u043a \u0447\u0438\u0442\u0430\u044e\u0449\u0430\u044f \u0438 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0442\u044c \u043f\u0438\u0448\u0443\u0449\u0435\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c INSERT. \u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 a \u0443\u0436\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u043e \u0441\u043d\u0438\u043c\u043e\u043a \u0431\u0430\u0437\u044b, \u0430 b \u0443\u0441\u043f\u0435\u043b\u043e \u0435\u0451 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c. \u0414\u0430\u0442\u044c a \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u0432\u0435\u0440\u0445 \u0441\u0442\u0430\u0440\u043e\u0433\u043e \u0441\u043d\u0438\u043c\u043a\u0430 SQLite \u043d\u0435 \u043c\u043e\u0436\u0435\u0442, \u0438 \u0436\u0434\u0430\u0442\u044c \u0442\u0443\u0442 \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e, \u0441\u043d\u0438\u043c\u043e\u043a \u043e\u0442 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0441\u0432\u0435\u0436\u0435\u0435 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 busy handler \u0434\u0430\u0436\u0435 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f.\u0418\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0438\u0441\u0430\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u043e\u0431\u044a\u044f\u0432\u0438\u0442\u044c \u0435\u0451 \u043f\u0438\u0448\u0443\u0449\u0435\u0439.a.execute(&#171;BEGIN IMMEDIATE&#187;)n = a.execute(&#171;SELECT count(*) FROM orders &#187;              &#171;WHERE user_id = 1 AND status = &#8216;new'&#187;).fetchone()[0]if n &lt; 3:    a.execute(&#171;INSERT INTO orders(user_id, status) VALUES (1, &#8216;new&#8217;)&#187;)a.execute(&#171;COMMIT&#187;)BEGIN IMMEDIATE \u0431\u0435\u0440\u0451\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043d\u0430\u0447\u0430\u043b\u0435. \u0415\u0441\u043b\u0438 \u0435\u0451 \u0434\u0435\u0440\u0436\u0438\u0442 \u043a\u0442\u043e-\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0439, \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0447\u0435\u0441\u0442\u043d\u043e \u0436\u0434\u0451\u0442 timeout, \u0430 \u043d\u0435 \u043f\u0430\u0434\u0430\u0435\u0442. \u042f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u043b \u043d\u0430 \u0434\u0432\u0443\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445: \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0430 \u0441\u0432\u043e\u0451\u043c BEGIN IMMEDIATE \u043f\u043e\u0434\u043e\u0436\u0434\u0430\u043b 0.52 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u043f\u043e\u043a\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442, \u0438 \u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u043b. \u0417\u0430\u043e\u0434\u043d\u043e \u043f\u0440\u043e\u043f\u0430\u043b\u0430 \u0433\u043e\u043d\u043a\u0430, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043e\u0431\u0430 \u0432\u0438\u0434\u0435\u043b\u0438 \u00ab\u0434\u0432\u0430 \u0437\u0430\u043a\u0430\u0437\u0430\u00bb \u0438 \u043e\u0431\u0430 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0438 \u0442\u0440\u0435\u0442\u0438\u0439.\u0415\u0441\u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435\u0441\u044c \u043d\u0435\u044f\u0432\u043d\u044b\u043c\u0438 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u043c\u043e\u0434\u0443\u043b\u044f, \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c isolation_level=&#187;IMMEDIATE&#187; \u0432 connect(), \u0442\u043e\u0433\u0434\u0430 \u043c\u043e\u0434\u0443\u043b\u044c \u0441\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c BEGIN IMMEDIATE \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e BEGIN. \u041d\u043e \u043d\u0435\u044f\u0432\u043d\u044b\u0439 BEGIN \u0441\u0442\u0430\u0432\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u0434 DML, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 SELECT \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u00ab\u043f\u0440\u043e\u0432\u0435\u0440\u044c \u0438 \u0437\u0430\u043f\u0438\u0448\u0438\u00bb \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432\u043d\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438. \u042f \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u044e isolation_level=None \u0438 \u044f\u0432\u043d\u044b\u0439 BEGIN IMMEDIATE: \u0442\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0432 \u043a\u043e\u0434\u0435, \u0433\u0434\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f.\u041f\u0440\u0438\u0447\u0438\u043d\u0430 3. \u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 sqlite3 \u0432\u043d\u0443\u0442\u0440\u0438 asyncio\u0421\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u0440\u0430\u0439\u0432\u0435\u0440 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0451\u0442, \u0437\u0430\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0451 \u0432 \u0440\u0430\u0437\u044b \u0431\u043e\u043b\u044c\u043d\u0435\u0435. \u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 execute() \u0436\u0434\u0451\u0442 \u0441\u0432\u043e\u0438 5 \u0441\u0435\u043a\u0443\u043d\u0434 \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u0446\u0438\u043a\u043b\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439. \u0412\u0435\u0441\u044c \u0431\u043e\u0442 \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u0442\u043e\u0438\u0442: \u043d\u0435 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f\u043c, \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043b\u0431\u044d\u043a\u0438, \u043d\u0435 \u0448\u043b\u0451\u0442 \u043d\u0438\u0447\u0435\u0433\u043e.\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430:import asyncio, sqlite3, threading, timedef hold_lock():    c = sqlite3.connect(&#171;shop.db&#187;)    c.execute(&#171;INSERT INTO orders(user_id, status) VALUES (7, &#8216;new&#8217;)&#187;)    time.sleep(3)    c.commit()async def ticker():    for _ in range(6):        print(time.strftime(&#171;%X&#187;), &#171;\u0442\u0438\u043a&#187;)        await asyncio.sleep(0.5)async def handler():    await asyncio.sleep(0.2)    conn = sqlite3.connect(&#171;shop.db&#187;, timeout=5)    conn.execute(&#171;INSERT INTO orders(user_id, status) VALUES (8, &#8216;new&#8217;)&#187;)    conn.commit()    print(time.strftime(&#171;%X&#187;), &#171;\u0437\u0430\u043f\u0438\u0441\u0430\u043b&#187;)async def main():    threading.Thread(target=hold_lock).start()    await asyncio.gather(ticker(), handler())asyncio.run(main())\u0412\u044b\u0432\u043e\u0434:09:47:07 \u0442\u0438\u043a09:47:10 \u0437\u0430\u043f\u0438\u0441\u0430\u043b09:47:10 \u0442\u0438\u043a09:47:10 \u0442\u0438\u043a09:47:11 \u0442\u0438\u043a&#8230;\u041c\u0435\u0436\u0434\u0443 \u043f\u0435\u0440\u0432\u044b\u043c \u0438 \u0432\u0442\u043e\u0440\u044b\u043c \u0442\u0438\u043a\u043e\u043c \u0442\u0440\u0438 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u0432\u043c\u0435\u0441\u0442\u043e \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u044b. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0431\u043e\u0442\u0435 \u044d\u0442\u0438 \u0442\u0440\u0438 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0432\u0441\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0440\u0430\u0437\u043e\u043c, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0442, \u043a\u0442\u043e \u043d\u0430\u0436\u0430\u043b \u043a\u043d\u043e\u043f\u043a\u0443.\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0434\u0440\u0430\u0439\u0432\u0435\u0440. aiosqlite \u0433\u043e\u043d\u044f\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438 \u043e\u0442\u0434\u0430\u0451\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0446\u0438\u043a\u043b\u0443, \u043f\u043e\u043a\u0430 \u0436\u0434\u0451\u0442. \u0424\u0430\u0439\u043b \u043e\u043d \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442, \u043d\u043e \u0431\u043e\u0442 \u0445\u043e\u0442\u044f \u0431\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c.\u0427\u0442\u043e \u044f \u0441\u0442\u0430\u0432\u043b\u044e \u0442\u0435\u043f\u0435\u0440\u044c \u0432 \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u043e\u0442 \u043d\u0430 SQLite\u0421\u043e\u0431\u0440\u0430\u043b \u0432\u0441\u0451 \u0432 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043a\u043b\u0430\u0441\u0441. \u041e\u0434\u043d\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, WAL, busy_timeout, \u044f\u0432\u043d\u044b\u0435 \u043f\u0438\u0448\u0443\u0449\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438.import asynciofrom contextlib import asynccontextmanagerimport aiosqliteclass Database:    def __init__(self, path: str):        self.path = path        self.conn: aiosqlite.Connection | None = None        self._write_lock = asyncio.Lock()    async def connect(self):        # isolation_level=None: \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u043c\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u0430\u043c\u0438        self.conn = await aiosqlite.connect(self.path, isolation_level=None)        await self.conn.execute(&#171;PRAGMA journal_mode=WAL&#187;)        await self.conn.execute(&#171;PRAGMA synchronous=NORMAL&#187;)        await self.conn.execute(&#171;PRAGMA busy_timeout=5000&#187;)        await self.conn.execute(&#171;PRAGMA foreign_keys=ON&#187;)    async def close(self):        await self.conn.close()    @asynccontextmanager    async def write(self):        async with self._write_lock:            await self.conn.execute(&#171;BEGIN IMMEDIATE&#187;)            try:                yield self.conn            except BaseException:                await self.conn.execute(&#171;ROLLBACK&#187;)                raise            else:                await self.conn.execute(&#171;COMMIT&#187;)    async def fetchone(self, sql, params=()):        async with self.conn.execute(sql, params) as cur:            return await cur.fetchone()\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0445\u0435\u043d\u0434\u043b\u0435\u0440\u0435:async def create_order(db: Database, user_id: int) -&gt; bool:    async with db.write() as conn:        row = await db.fetchone(            &#171;SELECT count(*) FROM orders WHERE user_id = ? AND status = &#8216;new'&#187;,            (user_id,),        )        if row[0] &gt;= 3:            return False        await conn.execute(            &#171;INSERT INTO orders(user_id, status) VALUES (?, &#8216;new&#8217;)&#187;, (user_id,)        )        return True\u041f\u0440\u043e\u0433\u043d\u0430\u043b 500 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 create_order \u0447\u0435\u0440\u0435\u0437 asyncio.gather \u043d\u0430 10 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. \u0412 \u0431\u0430\u0437\u0435 \u0440\u043e\u0432\u043d\u043e 30 \u0437\u0430\u043a\u0430\u0437\u043e\u0432, \u043f\u043e \u0442\u0440\u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e, \u043d\u0438 \u043e\u0434\u043d\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u0438. \u0418\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 write() \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e, \u044d\u0442\u043e \u0442\u043e\u0436\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043b.\u0422\u0435\u043f\u0435\u0440\u044c \u0437\u0430\u0447\u0435\u043c \u0442\u0430\u043c \u043a\u0430\u0436\u0434\u0430\u044f \u0441\u0442\u0440\u043e\u0447\u043a\u0430.\u041f\u0440\u043e WAL. \u0412 \u0440\u0435\u0436\u0438\u043c\u0435 \u0436\u0443\u0440\u043d\u0430\u043b\u0430 \u043f\u0440\u0435\u0434\u0437\u0430\u043f\u0438\u0441\u0438 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043d\u0435 \u043c\u0435\u0448\u0430\u044e\u0442 \u043f\u0438\u0441\u0430\u0442\u0435\u043b\u044e \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442. \u041a\u0440\u043e\u043d \u0441 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u0432 Excel \u043c\u043e\u0436\u0435\u0442 \u0447\u0438\u0442\u0430\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0433\u043e\u0434\u043d\u043e, \u0431\u043e\u0442 \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0438\u0448\u0435\u0442. \u041f\u0438\u0441\u0430\u0442\u0435\u043b\u044c \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043e\u0434\u0438\u043d, \u0434\u043b\u044f \u0431\u043e\u0442\u0430 \u044d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430. \u0420\u0435\u0436\u0438\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0444\u0430\u0439\u043b\u0435 \u0431\u0430\u0437\u044b, \u043d\u043e PRAGMA \u0432 connect() \u044f \u0432\u0441\u0451 \u0440\u0430\u0432\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e. \u0420\u044f\u0434\u043e\u043c \u0441 \u0431\u0430\u0437\u043e\u0439 \u043f\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u0444\u0430\u0439\u043b\u044b -wal \u0438 -shm, \u044d\u0442\u043e \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e. \u0422\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0431\u044d\u043a\u0430\u043f\u0430 \u043e\u0434\u0438\u043d .db, \u043f\u043e\u043a\u0430 \u0431\u043e\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u0435\u043b\u044c\u0437\u044f: \u0441\u0432\u0435\u0436\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u0432 -wal. \u0414\u043b\u044f \u0431\u044d\u043a\u0430\u043f\u0430 \u0435\u0441\u0442\u044c conn.backup() \u0438\u043b\u0438 VACUUM INTO. \u0418 WAL \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430 \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u0444\u0430\u0439\u043b\u043e\u0432\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u0430\u0445, \u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e\u043c Docker volume \u0432\u0441\u0451 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435.synchronous=NORMAL \u0432 WAL \u044d\u0442\u043e \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441: \u043f\u0440\u0438 \u043f\u0430\u0434\u0435\u043d\u0438\u0438 \u043f\u0438\u0442\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438, \u043d\u043e \u0431\u0430\u0437\u0430 \u043d\u0435 \u043f\u043e\u0431\u044c\u0451\u0442\u0441\u044f. \u0414\u043b\u044f \u0431\u043e\u0442\u0430 \u0441 \u0437\u0430\u043a\u0430\u0437\u0430\u043c\u0438 \u043c\u0435\u043d\u044f \u044d\u0442\u043e \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442. \u0415\u0441\u043b\u0438 \u0434\u043b\u044f \u0432\u0430\u0441 \u043d\u0435 \u0443\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442, \u0443\u0431\u0435\u0440\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u043a\u0443.busy_timeout \u044d\u0442\u043e \u0442\u043e\u0442 \u0436\u0435 timeout \u0438\u0437 connect(), \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445 \u0438 \u0447\u0435\u0440\u0435\u0437 PRAGMA. \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432: \u0442\u043e\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043a\u0440\u043e\u043d\u0430 \u0438\u043b\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u0430 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b\u0438 \u0440\u0443\u043a\u0430\u043c\u0438 \u043d\u0430 \u043f\u0440\u043e\u0434\u0435.asyncio.Lock \u043d\u0443\u0436\u0435\u043d, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e, \u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043c\u043d\u043e\u0433\u043e. \u0411\u0435\u0437 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u0432\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043f\u043e\u043f\u044b\u0442\u0430\u044e\u0442\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u044c BEGIN IMMEDIATE \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u0438 \u0442\u043e\u043c \u0436\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438, \u0438 \u0432\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442 cannot start a transaction within a transaction. \u042f \u043d\u0430 \u044d\u0442\u043e \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u043b, \u043a\u043e\u0433\u0434\u0430 \u0441\u043e\u0431\u0438\u0440\u0430\u043b \u043f\u0440\u0438\u043c\u0435\u0440. Lock \u0432\u044b\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-500429","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/500429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=500429"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/500429\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=500429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=500429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=500429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}