Skip to content

Force task switch every 2000 rows when creating objects #1939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

henadzit
Copy link
Contributor

@henadzit henadzit commented Apr 24, 2025

Description

When selecting a large number of records, the process of converting database records to Tortoise objects might completely block the event loop. This PR introduces a task switch every 2000 rows to allow other workloads.

Motivation and Context

Fixes #1378.
I remember seeing other similar issues.

How Has This Been Tested?

make ci and testing it in my hobby project.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@henadzit henadzit requested a review from Copilot April 24, 2025 05:41
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses event loop blocking by forcing a task switch every 2000 rows while processing large result sets.

  • Introduces a CHUNK_SIZE constant to define the row interval for yielding.
  • Modifies the loop over raw results to use enumerate and conditionally await asyncio.sleep(0) for a task switch.

Copy link

codspeed-hq bot commented Apr 24, 2025

CodSpeed Performance Report

Merging #1939 will not alter performance

Comparing henadzit:force-task-switch-when-loading (11626dc) with develop (503c93b)

Summary

✅ 16 untouched benchmarks

@coveralls
Copy link

coveralls commented Apr 24, 2025

Pull Request Test Coverage Report for Build 14718896345

Details

  • 3 of 4 (75.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 89.656%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/backends/base/executor.py 3 4 75.0%
Totals Coverage Status
Change from base Build 14444702854: -0.02%
Covered Lines: 6670
Relevant Lines: 7259

💛 - Coveralls

abondar
abondar previously approved these changes Apr 25, 2025
@henadzit henadzit force-pushed the force-task-switch-when-loading branch from bac6883 to 11626dc Compare April 28, 2025 22:12
@henadzit henadzit marked this pull request as ready for review April 28, 2025 22:12
@henadzit henadzit requested a review from abondar April 28, 2025 22:12
@@ -24,10 +30,6 @@ Added
^^^^^
- `.only` supports selecting related fields, e.g. `.only("related__field")` (#1923)

Fixed
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed a duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Queries are blocking the main loop
3 participants