tortoise / tortoise/tortoise-orm

Tortoise.generate_schemas raises asyncio Warning (took too much time)

Open
#881 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
Hello everyone! I was using (and enjoying) this awesome orm, and I've found this issue:

calling Tortoise.generate_schemas raises the warning:

WARNING:asyncio:Executing <Task pending name='Task-1' coro=<init() running at /opt/venvs/[...]/lib/python3.8/site-packages/[...]/db.py:35> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f49452f2b80>()] created at /usr/lib/python3.8/asyncio/base_events.py:422> cb=[_run_until_complete_cb() at /usr/lib/python3.8/asyncio/base_events.py:184] created at /usr/lib/python3.8/asyncio/base_events.py:595> took 0.225 seconds

where our init() coro, defined in our [...]/db.py:35 file, contains the line await Tortoise.generate_schemas(safe=True)

I think that probably this happens because generate_schema_for_client inside is synchronously calling to get_schema_sql, which takes too long and that freezes the event loop.

To Reproduce
We have ~25 tables in our database.
And have the debug mode enabled.

Expected behavior
The method generate_schemas should not block the event loop with synchronous code

Additional context
our Pipfile contains this versions

[packages]
aerich = "==0.5.3"
tortoise-orm = "~=0.17.5"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Tortoise.generate_schemas and follow its generate_schema_for_client call into get_schema_sql. Reproduce with roughly 25 tables, debug mode enabled, and the reported Tortoise ORM versions, then check whether schema generation blocks the event loop. Done means the warning no longer appears and generate_schemas does not block asynchronous work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.