Skip to content

misc: simplify running non-async callback using asyncio.to_thread#1661

Merged
JrooTJunior merged 2 commits intoaiogram:dev-3.xfrom
loRes228:dev-3.x
Apr 2, 2025
Merged

misc: simplify running non-async callback using asyncio.to_thread#1661
JrooTJunior merged 2 commits intoaiogram:dev-3.xfrom
loRes228:dev-3.x

Conversation

@loRes228
Copy link
Contributor

Description

Replaced loop.run_in_executor with asyncio.to_thread for improved readability and consistency.
This change does not affect functionality but simplifies the code.

No related issue.

Type of change

  • Miscellaneous (code cleanup, refactoring, minor improvements)

How Has This Been Tested?

Since this is a non-functional change, no new tests were required.
Existing tests were run to ensure there were no regressions.

Test Configuration:

  • Operating System:
  • Python version:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • Existing unit tests pass locally with my changes

@github-actions github-actions bot added the 3.x Issue or PR for stable 3.x version label Mar 31, 2025
@github-actions
Copy link

github-actions bot commented Mar 31, 2025

✔️ Changelog found.

Thank you for adding a description of the changes

context = contextvars.copy_context()
wrapped = partial(context.run, wrapped)
return await loop.run_in_executor(None, wrapped)
return await asyncio.to_thread(wrapped)
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure copy context is not necessary anymore?

Copy link
Contributor Author

@loRes228 loRes228 Apr 1, 2025

Choose a reason for hiding this comment

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

asyncio.to_thread does the same thing, gets the current loop, context and runs it in the executor.

@loRes228 loRes228 requested a review from Olegt0rr April 1, 2025 10:16
@codecov
Copy link

codecov bot commented Apr 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (7e8dcc6) to head (b76cc2b).
Report is 2 commits behind head on dev-3.x.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           dev-3.x     #1661   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          511       511           
  Lines        12531     12528    -3     
=========================================
- Hits         12531     12528    -3     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
aiogram/dispatcher/event/handler.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JrooTJunior JrooTJunior merged commit 02683b8 into aiogram:dev-3.x Apr 2, 2025
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.x Issue or PR for stable 3.x version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants