Skip to content

Commit 2489914

Browse files
authored
Force task switch every 2000 rows when creating objects (#1939)
1 parent 503c93b commit 2489914

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ Changelog
99
0.25
1010
====
1111

12+
0.25.1 (unreleased)
13+
------------------
14+
Changed
15+
^^^^^
16+
- Force async task switch every 2000 rows when converting db objects to python objects to avoid blocking the event loop (#1939)
17+
1218
0.25.0
1319
------
1420
Fixed
@@ -24,10 +30,6 @@ Added
2430
^^^^^
2531
- `.only` supports selecting related fields, e.g. `.only("related__field")` (#1923)
2632

27-
Fixed
28-
^^^^^
29-
- Fix pydantic_model_creator incompatibility with Pydantic 2.11 (#1930)
30-
3133

3234
0.24
3335
====

tortoise/backends/base/executor.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
tuple[list, str, list, str, str, dict[str, str]],
3333
] = {}
3434

35+
CHUNK_SIZE = 2000
36+
3537

3638
class BaseExecutor:
3739
FILTER_FUNC_OVERRIDE: dict[Callable, Callable] = {}
@@ -105,7 +107,12 @@ async def execute_select(
105107
) -> list:
106108
_, raw_results = await self.db.execute_query(sql, values)
107109
instance_list = []
108-
for row in raw_results:
110+
for row_idx, row in enumerate(raw_results):
111+
if row_idx != 0 and row_idx % CHUNK_SIZE == 0:
112+
# Forcibly yield to the event loop to avoid blocking the event loop
113+
# when selecting a large number of rows
114+
await asyncio.sleep(0)
115+
109116
if self.select_related_idx:
110117
_, current_idx, _, _, path = self.select_related_idx[0]
111118
row_items = list(dict(row).items())

0 commit comments

Comments
 (0)