You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 KiB

🗄 (🔗) 💽 ⏮️ 🏒

!!! warning 🚥 👆 ▶️, 🔰 🗄 (🔗) 💽{.internal-link target=_blank} 👈 ⚙️ 🇸🇲 🔜 🥃.

💭 🆓 🚶 👉.

🚥 👆 ▶️ 🏗 ➡️ 🖌, 👆 🎲 👻 📆 ⏮️ 🇸🇲 🐜 (🗄 (🔗) 💽{.internal-link target=_blank}), ⚖️ 🙆 🎏 🔁 🐜.

🚥 👆 ✔️ 📟 🧢 👈 ⚙️ 🏒 🐜, 👆 💪 📥 ⚙️ ⏮️ FastAPI.

!!! warning "🐍 3️⃣.7️⃣ ✔" 👆 🔜 💪 🐍 3️⃣.7️⃣ ⚖️ 🔛 🔒 ⚙️ 🏒 ⏮️ FastAPI.

🏒 🔁

🏒 🚫 🔧 🔁 🛠️, ⚖️ ⏮️ 👫 🤯.

🏒 ✔️ 🏋️ 🔑 🔃 🚮 🔢 & 🔃 🔜 ⚙️.

🚥 👆 🛠️ 🈸 ⏮️ 🗝 🚫-🔁 🛠️, & 💪 👷 ⏮️ 🌐 🚮 🔢, 💪 👑 🧰.

🚥 👆 💪 🔀 🔢, 🐕‍🦺 🌖 🌘 1️⃣ 🔁 💽, 👷 ⏮️ 🔁 🛠️ (💖 FastAPI), ️, 👆 🔜 💪 🚮 🏗 📟 🔐 👈 🔢.

👐, 💪 ️, & 📥 👆 🔜 👀 📟 👆 ✔️ 🚮 💪 ⚙️ 🏒 ⏮️ FastAPI.

!!! note "📡 ℹ" 👆 💪🌅 🔃 🏒 🧍 🔃 🔁 🐍 🩺, , 🇵🇷.

🎏 📱

👥 🔜🎏 🈸 🇸🇲 🔰 (🗄 (🔗) 💽{.internal-link target=_blank}).

🌅 📟 🤙 🎏.

, 👥 🔜 🎯 🕴 🔛 🔺.

📁 📊

➡️ 💬 👆 ✔️ 📁 📛 my_super_project 👈 🔌 🎧-📁 🤙 sql_app ⏮️ 📊 💖 👉:

.
└── sql_app
    ├── __init__.py
    ├── crud.py
    ├── database.py
    ├── main.py
    └── schemas.py

👉 🌖 🎏 📊 👥 ✔️ 🇸🇲 🔰.

🔜 ➡️ 👀 🔠 📁/🕹 🔨.

🏒 🍕

➡️ 🔗 📁 sql_app/database.py.

🐩 🏒 📟

➡️ 🥇 🌐 😐 🏒 📟, ✍ 🏒 💽:

{!../../../docs_src/sql_databases_peewee/sql_app/database.py!}

!!! tip ✔️ 🤯 👈 🚥 👆 💚 ⚙️ 🎏 💽, 💖 ✳, 👆 🚫 🚫 🔀 🎻. 👆 🔜 💪 ⚙️ 🎏 🏒 💽 🎓.

🗒

:

check_same_thread=False

🌓 1️⃣ 🇸🇲 🔰:

connect_args={"check_same_thread": False}

...💪 🕴 SQLite.

!!! info "📡 ℹ"

⚫️❔ 🎏 📡 ℹ [🗄 (🔗) 💽](../tutorial/sql-databases.md#_7){.internal-link target=_blank} ✔.

🏒 🔁-🔗 PeeweeConnectionState

👑 ⏮️ 🏒 & FastAPI 👈 🏒 🙇 🔛 🐍 threading.local, & 🚫 ✔️ 🎯 🌌 🔐 ⚖️ ➡️ 👆 🍵 🔗/🎉 🔗 (🔨 🇸🇲 🔰).

& threading.local 🚫 🔗 ⏮️ 🆕 🔁 ⚒ 🏛 🐍.

!!! note "📡 ℹ" threading.local ⚙️ ✔️ "🎱" 🔢 👈 ✔️ 🎏 💲 🔠 🧵.

👉 ⚠ 🗝 🛠️ 🏗 ✔️ 1️⃣ 👁 🧵 📍 📨, 🙅‍♂ 🌖, 🙅‍♂ 🌘.

⚙️ 👉, 🔠 📨 🔜 ✔️ 🚮 👍 💽 🔗/🎉, ❔ ☑ 🏁 🥅.

✋️ FastAPI, ⚙️ 🆕 🔁 ⚒, 💪 🍵 🌅 🌘 1️⃣ 📨 🔛 🎏 🧵. & 🎏 🕰, 👁 📨, ⚫️ 💪 🏃 💗 👜 🎏 🧵 (🧵), ⚓️ 🔛 🚥 👆 ⚙️ `async def` ⚖️ 😐 `def`. 👉 ⚫️❔ 🤝 🌐 🎭 📈 FastAPI.

🐍 3️⃣.7️⃣ & 🔛 🚚 🌖 🏧 🎛 threading.local, 👈 💪 ⚙️ 🥉 🌐 threading.local 🔜 ⚙️, 🔗 ⏮️ 🆕 🔁 ⚒.

👥 🔜 ⚙️ 👈. 🤙 contextvars.

👥 🔜 🔐 🔗 🍕 🏒 👈 ⚙️ threading.local & 👫 ⏮️ contextvars, ⏮️ 🔗 ℹ.

👉 5️⃣📆 😑 🍖 🏗 (& 🤙), 👆 🚫 🤙 💪 🍕 🤔 👷 ⚙️ ️.

👥 🔜PeeweeConnectionState:

{!../../../docs_src/sql_databases_peewee/sql_app/database.py!}

👉 🎓 😖 ➡️ 🎁 🔗 🎓 ⚙️ 🏒.

✔️ 🌐 ⚛ ⚒ 🏒 ⚙️ contextvars ↩️ threading.local.

contextvars 👷 🍖 🎏 🌘 threading.local. 🎂 🏒 🔗 📟 🤔 👈 👉 🎓 👷 ⏮️ threading.local.

, 👥 💪 🎱👷 🚥 ⚙️ threading.local. __init__, __setattr__, & __getattr__ 🛠️ 🌐🎱 👉 ⚙️ 🏒 🍵 🤔 👈 🔜 🔗 ⏮️ FastAPI.

!!! tip 👉 🔜🏒 🎭🕐 ⚙️ ⏮️ FastAPI. 🚫 🎲 📂 ⚖️ 📪 🔗 👈 ⚙️, 🏗 , ️.

✋️ ⚫️ 🚫 🤝 🏒 🔁 💎-🏋️. 👆 🔜 ⚙️ 😐 `def` 🔢 & 🚫 `async def`.

⚙️ 🛃 PeeweeConnectionState 🎓

🔜, 📁 ._state 🔗 🔢 🏒 💽 db 🎚 ⚙️ 🆕 PeeweeConnectionState:

{!../../../docs_src/sql_databases_peewee/sql_app/database.py!}

!!! tip ⚒ 💭 👆 📁 db._state ⏮️ 🏗 db.

!!! tip 👆 🔜 🎏 🙆 🎏 🏒 💽, 🔌 PostgresqlDatabase, MySQLDatabase, ️.

💽 🏷

➡️ 🔜 👀 📁 sql_app/models.py.

🏒 🏷 👆 💽

🔜🏒 🏷 (🎓) User & Item.

👉 🎏 👆 🔜 🚥 👆 🏒 🔰 & ℹ 🏷 ✔️ 🎏 💽 🇸🇲 🔰.

!!! tip 🏒 ⚙️ ⚖ "🏷" 🔗 👉 🎓 & 👐 👈 🔗 ⏮️ 💽.

✋️ Pydantic ⚙️ ⚖ "**🏷**" 🔗 🕳 🎏, 💽 🔬, 🛠️, & 🧾 🎓 & 👐.

🗄 db ➡️ database (📁 database.py ➡️ 🔛) & ⚙️ 📥.

{!../../../docs_src/sql_databases_peewee/sql_app/models.py!}

!!! tip 🏒📚 🎱 🔢.

⚫️ 🔜 🔁 🚮 `id` 🔢 🔢 👑 🔑.

⚫️ 🔜 ⚒ 📛 🏓 ⚓️ 🔛 🎓 📛.

 `Item`, ⚫️ 🔜 ✍ 🔢 `owner_id` ⏮️ 🔢 🆔 `User`. ✋️ 👥 🚫 📣 ⚫️ 🙆.

✍ Pydantic 🏷

🔜 ➡️ 📁 sql_app/schemas.py.

!!! tip 😨 🖖 🏒 🏷 & Pydantic 🏷, 👥 🔜 ✔️ 📁 models.py ⏮️ 🏒 🏷, & 📁 schemas.py ⏮️ Pydantic 🏷.

👫 Pydantic 🏷 🔬 🌅 ⚖️ 🌘 "🔗" (☑ 📊 💠).

👉 🔜 ℹ 👥 ❎ 😨 ⏪ ⚙️ 👯‍♂️.

✍ Pydantic 🏷 / 🔗

🌐 🎏 Pydantic 🏷 🇸🇲 🔰:

{!../../../docs_src/sql_databases_peewee/sql_app/schemas.py!}

!!! tip 📥 👥 🏗 🏷 ⏮️ id.

👥 🚫 🎯 ✔ `id` 🔢 🏒 🏷, ✋️ 🏒 🚮 1️⃣ 🔁.

👥 ❎ 🎱 `owner_id` 🔢 `Item`.

PeeweeGetterDict Pydantic 🏷 / 🔗

🕐 👆 🔐 💛 🏒 🎚, 💖 some_user.items, 🏒 🚫 🚚 list Item.

🚚 🎁 🛃 🎚 🎓 ModelSelect.

💪list 🚮 🏬 ⏮️ list(some_user.items).

️ 🎚 🚫 list. & 🚫🐍 🚂. ↩️ 👉, Pydantic 🚫 💭 🔢 🗜 list Pydantic 🏷 / 🔗.

⏮️ Pydantic ✔ 🚚 🛃 🎓 👈 😖 ➡️ pydantic.utils.GetterDict, 🚚 🛠️ ⚙️ 🕐 ⚙️ orm_mode = True 🗃 💲 🐜 🏷 🔢.

👥 🔜🛃 PeeweeGetterDict 🎓 & ⚙️ 🌐 🎏 Pydantic 🏷 / 🔗 👈 ⚙️ orm_mode:

{!../../../docs_src/sql_databases_peewee/sql_app/schemas.py!}

📥 👥 🚥 🔢 👈 🔐 ( .items some_user.items) 👐 peewee.ModelSelect.

& 🚥 👈 💼, 📨 list ⏮️ ️.

& ⤴️ 👥 ⚙️ ️ Pydantic 🏷 / 🔗 👈 ⚙️ orm_mode = True, ⏮️ 📳 🔢 getter_dict = PeeweeGetterDict.

!!! tip 👥 🕴 💪1️⃣ PeeweeGetterDict 🎓, & 👥 💪 ⚙️ 🌐 Pydantic 🏷 / 🔗.

💩 🇨🇻

🔜 ➡️ 👀 📁 sql_app/crud.py.

🌐 💩 🇨🇻

🌐 🎏 💩 🇨🇻 🇸🇲 🔰, 🌐 📟 📶 🎏:

{!../../../docs_src/sql_databases_peewee/sql_app/crud.py!}

📤 🔺 ⏮️ 📟 🇸🇲 🔰.

👥 🚫 🚶‍♀️ db 🔢 🤭. ↩️ 👥 ⚙️ 🏷 🔗. 👉 ↩️ db 🎚 🌐 🎚, 👈 🔌 🌐 🔗 ⚛. 👈 👥 ✔️ 🌐 contextvars🔛.

🆖, 🕐 🛬 📚 🎚, 💖 get_users, 👥 🔗 🤙 list, 💖:

list(models.User.select())

👉 🎏 🤔 👈 👥 ✔️🛃 PeeweeGetterDict. 🛬 🕳 👈 list ↩️ peewee.ModelSelect response_model 🛠️ ⏮️ List[models.User] (👈 👥 🔜 👀 ) 🔜 👷 ☑.

👑 FastAPI 📱

& 🔜 📁 sql_app/main.py ➡️ 🛠️ & ⚙️ 🌐 🎏 🍕 👥 ✍ ⏭.

💽 🏓

📶 🙃 🌌💽 🏓:

{!../../../docs_src/sql_databases_peewee/sql_app/main.py!}

🔗

🔗 👈 🔜 🔗 💽 ▶️▶️ 📨 & 🔌 🔚:

{!../../../docs_src/sql_databases_peewee/sql_app/main.py!}

📥 👥 ✔️ 🛁 yield ↩️ 👥 🤙 🚫 ⚙️ 💽 🎚 🔗.

🔗 💽 & ♻ 🔗 💽 🔗 🔢 👈 🔬 🔠 📨 (⚙️ contextvars 🎱 ➡️ 🔛).

↩️ 💽 🔗👤/🅾 🚧, 👉 🔗⏮️ 😐 def 🔢.

& ⤴️, 🔠 🛠️ 🔢 👈 💪 🔐 💽 👥 🚮 🔗.

👥 🚫 ⚙️ 💲 👐 👉 🔗 (🤙 🚫 🤝 🙆 💲, ✔️ 🛁 yield). , 👥 🚫 🚮 🛠️ 🔢 🛠️ 👨‍🎨 dependencies 🔢:

{!../../../docs_src/sql_databases_peewee/sql_app/main.py!}

🔑 🔢 🎧-🔗

🌐 contextvars 🍕 👷, 👥 💪💭 👥 ✔️ 🔬 💲 ContextVar 🔠 📨 👈 ⚙️ 💽, & 👈 💲 🔜 ⚙️ 💽 🇵🇸 (🔗, 💵, ️) 🎂 📨.

👈, 👥 💪1️⃣ async 🔗 reset_db_state() 👈 ⚙️ 🎧-🔗 get_db(). 🔜💲 🔑 🔢 (⏮️ 🔢 dict) 👈 🔜 ⚙️ 💽 🇵🇸 🎂 📨. & ⤴️ 🔗 get_db() 🔜 🏪 💽 🇵🇸 (🔗, 💵, ️).

{!../../../docs_src/sql_databases_peewee/sql_app/main.py!}

📨, 👥 🔜👈 🔑 🔢 🔄 async 🔗 reset_db_state() & ⤴️🆕 🔗 get_db() 🔗, 👈 🆕 📨 🔜 ✔️ 🚮 👍 💽 🇵🇸 (🔗, 💵, ️).

!!! tip FastAPI 🔁 🛠️, 1️⃣ 📨 💪 ▶️ 🛠️, & ⏭ 🏁, 1️⃣ 📨 💪 📨 & ▶️ 🏭 👍, & 🌐 💪 🛠️ 🎏 🧵.

✋️ 🔑 🔢 🤔 👫 🔁 ⚒,, 🏒 💽 🇵🇸 ⚒ `async` 🔗 `reset_db_state()` 🔜 🚧 🚮 👍 💽 🎂 🎂 📨.

 & 🎏 🕰, 🎏 🛠️ 📨 🔜 ✔️ 🚮 👍 💽 🇵🇸 👈 🔜 🔬 🎂 📨.

🏒 🗳

🚥 👆 ⚙️ 🏒 🗳, ☑ 💽 db.obj.

, 👆 🔜⏮️:

async def reset_db_state():
    database.db.obj._state._state.set(db_state_default.copy())
    database.db.obj._state.reset()

👆 FastAPI 🛠️

🔜, 😒, 📥 🐩 FastAPI 🛠️ 📟.

{!../../../docs_src/sql_databases_peewee/sql_app/main.py!}

🔃 def 🆚 async def

🎏 ⏮️ 🇸🇲, 👥 🚫 🔨 🕳 💖:

user = await models.User.select().first()

...↩️ 👥 ⚙️:

user = models.User.select().first()

, 🔄, 👥 🔜 📣 🛠️ 🔢 & 🔗 🍵 async def, ⏮️ 😐 def,:

# Something goes here
def read_users(skip: int = 0, limit: int = 100):
    # Something goes here

🔬 🏒 ⏮️ 🔁

👉 🖼 🔌 🛠️ 👈 🔬 📏 🏭 📨 ⏮️ time.sleep(sleep_time).

🔜 ✔️ 💽 🔗 📂 ▶️ & 🔜 🥈🙇 🔙. & 🔠 🆕 📨 🔜 🕐 🥈 🌘.

👉 🔜 💪 ➡️ 👆 💯 👈 👆 📱 ⏮️ 🏒 & FastAPI 🎭⏮️ 🌐 💩 🔃 🧵.

🚥 👆 💚 🏒 🔜 💔 👆 📱 🚥 ⚙️ 🍵 🛠️, 🚶 sql_app/database.py 📁 & 🏤 ⏸:

# db._state = PeeweeConnectionState()

& 📁 sql_app/main.py 📁, 🏤 💪 async 🔗 reset_db_state() & ⏮️ pass:

async def reset_db_state():
#     database.db._state._state.set(db_state_default.copy())
#     database.db._state.reset()
    pass

⤴️ 🏃 👆 📱 ⏮️ Uvicorn:

$ uvicorn sql_app.main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

📂 👆 🖥 http://127.0.0.1:8000/docs & ✍ 👩‍❤‍👨 👩‍💻.

⤴️ 📂 1️⃣0️⃣ 📑 http://127.0.0.1:8000/docs#/default/read_🐌_👩‍💻_slowusers_ = 🎏 🕰.

🚶 🛠️ "🤚 /slowusers/" 🌐 📑. ⚙️ "🔄 👅" 🔼 & 🛠️ 📨 🔠 📑, 1️⃣ ▶️⏮️ 🎏.

📑 🔜 🍖 & ⤴️ 👫 🔜 🎦 Internal Server Error.

🔨

🥇 📑 🔜👆 📱🔗 💽 & 🥈🙇 🔙 & 📪 💽 🔗.

⤴️, 📨📑, 👆 📱 🔜 🕐 🥈 🌘, & 🔛.

👉👈 🔜 🔚 🆙 🏁 🏁 📑' 📨 🌘 ⏮️ 🕐.

⤴️ 1️⃣ 🏁 📨 👈 🌘 🥈 🔜 🔄 📂 💽 🔗, 1️⃣ 📚 ⏮️ 📨 🎏 📑 🔜 🎲 🍵 🎏 🧵 🥇 🕐, 🔜 ✔️ 🎏 💽 🔗 👈 📂, & 🏒 🔜 🚮 & 👆 🔜 👀 📶, & 📨 🔜 ✔️ Internal Server Error.

👉 🔜 🎲 🔨 🌅 🌘 1️⃣ 📚 📑.

🚥 👆 ✔️ 💗 👩‍💻 💬 👆 📱 🎏 🕰, 👉 💪 🔨.

& 👆 📱 ▶️ 🍵 🌅 & 🌖 👩‍💻 🎏 🕰, 🕰 👁 📨 💪 📏 & 📏.

🔧 🏒 ⏮️ FastAPI

🔜 🚶 🔙 📁 sql_app/database.py, & ✍ ⏸:

db._state = PeeweeConnectionState()

& 📁 sql_app/main.py 📁, ✍ 💪 async 🔗 reset_db_state():

async def reset_db_state():
    database.db._state._state.set(db_state_default.copy())
    database.db._state.reset()

👆 🏃‍♂ 📱 & ▶️ 🔄.

🔁 🎏 🛠️ ⏮️ 1️⃣0️⃣ 📑. 👉 🕰 🌐 👫 🔜 & 👆 🔜 🤚 🌐 🏁 🍵 .

...👆 🔧

📄 🌐 📁

💭 👆 🔜 ✔️ 📁 📛 my_super_project (⚖️ 👐 👆 💚) 👈 🔌 🎧-📁 🤙 sql_app.

sql_app 🔜 ✔️ 📄 📁:

  • sql_app/__init__.py: 🛁 📁.

  • sql_app/database.py:

{!../../../docs_src/sql_databases_peewee/sql_app/database.py!}
  • sql_app/models.py:
{!../../../docs_src/sql_databases_peewee/sql_app/models.py!}
  • sql_app/schemas.py:
{!../../../docs_src/sql_databases_peewee/sql_app/schemas.py!}
  • sql_app/crud.py:
{!../../../docs_src/sql_databases_peewee/sql_app/crud.py!}
  • sql_app/main.py:
{!../../../docs_src/sql_databases_peewee/sql_app/main.py!}

📡

!!! warning 👉 📶 📡👈 👆 🎲 🚫 💪.

🏒 ⚙️ threading.local 🔢 🏪 💽 "🇵🇸" 💽 (🔗, 💵, ️).

threading.local💲 🌟 ⏮️ 🧵, 🔁 🛠️ 🔜 🏃 🌐 📟 ( 🔠 📨) 🎏 🧵, & 🎲 🚫 ✔.

🔛 🔝 👈, 🔁 🛠️ 💪 🏃 🔁 📟 🧵 (⚙️ asyncio.run_in_executor), 🔗 🎏 📨.

👉👈, ⏮️ 🏒 ⏮️ 🛠️, 💗 📋 💪 ⚙️ 🎏 threading.local 🔢 & 🔚 🆙 🤝 🎏 🔗 & 💽 (👈 👫 🚫🔜 🚫), & 🎏 🕰, 🚥 👫 🛠️ 🔁 👤/🅾-🚧 📟 🧵 (⏮️ 😐 def 🔢 FastAPI, 🛠️ & 🔗), 👈 📟 🏆 🚫 ✔️ 🔐 💽 🇵🇸 🔢, 🍕 🎏 📨 & 🔜 💪 🤚 🔐 🎏 💽 🇵🇸.

🔑 🔢

🐍 3️⃣.7️⃣ ✔️ contextvars 👈 💪🇧🇿 🔢 📶 🎏 threading.local, 🔗 👫 🔁 ⚒.

📤 📚 👜 ✔️ 🤯.

ContextVar ✔️🔝 🕹, 💖:

some_var = ContextVar("some_var", default="default value")

💲 ⚙️ ⏮️ "🔑" ( ⏮️ 📨) ⚙️:

some_var.set("new value")

🤚 💲 🙆 🔘 🔑 ( 🙆 🍕 🚚 ⏮️ 📨) ⚙️:

some_var.get()

🔑 🔢 async 🔗 reset_db_state()

🚥 🍕 🔁 📟💲 ⏮️ some_var.set("updated in function") ( 💖 async 🔗), 🎂 📟 ️ & 📟 👈 🚶 ⏮️ ( 📟 🔘 async 🔢 🤙 ⏮️ await) 🔜 👀 👈 🆕 💲.

, 👆 💼, 🚥 👥🏒 🇵🇸 🔢 (⏮️ 🔢 dict) async 🔗, 🌐 🎂 🔗 📟 👆 📱 🔜 👀 👉 💲 & 🔜 💪🎂 📨.

& 🔑 🔢 🔜🔄📨, 🚥 👫 🛠️.

💽 🇵🇸 🔗 get_db()

get_db() 😐 def 🔢, FastAPI 🔜🏃 🧵, ⏮️ 📁 "🔑", 🧑‍🤝‍🧑 🎏 💲 🔑 🔢 ( dict ⏮️💽 🇵🇸). ⤴️ 💪 🚮 💽 🇵🇸 👈 dict, 💖 🔗, ️.

🚥 💲 🔑 🔢 (🔢 dict) ⚒ 👈 😐 def 🔢, 🔜🆕 💲 👈 🔜 🚧 🕴 👈 🧵 🧵, & 🎂 📟 (💖 🛠️ 🔢) 🚫🔜 ✔️ 🔐 ️. get_db() 👥 💪 🕴 ⚒ 💲 dict, 🚫 🎂 dict ️.

, 👥 💪 ✔️ async 🔗 reset_db_state()dict 🔑 🔢. 👈 🌌, 🌐 📟 ✔️ 🔐 🎏 dict 💽 🇵🇸 👁 📨.

🔗 & 🔌 🔗 get_db()

⤴️ 🔜, 🚫 🔗 & 🔌 💽 async 🔗 ️, ↩️ get_db()

async 🔗 ✔️ async 🔑 🔢 🛡 🎂 📨, ️ 🏗 & 📪 💽 🔗🚧, 💪 📉 🎭 🚥 📤.

👥 💪 😐 def 🔗 get_db().