Skip to content

Enhancements to QThreadExecutor #152

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

kristjanvalur
Copy link
Contributor

@kristjanvalur kristjanvalur commented Aug 16, 2025

This PR enhances the conformity of QThreadExecutor with the Executor protocol.

  • shutdown() is now thread-safe and there is no race between shutdown() and submit().
  • shutdown() can be called more than once. This is how the standard Executors work, and is congruent with close() and similar methods.
  • map() is supported.
  • cancel_futures argument to shutdown() is supported.

In addition, a context manager, closing() is added, which does a shutdown(wait=False) by default. This is useful in code like

with QThreadExecutor(1).closing() as executor:
    await asyncio.get_running_loop().run_in_executor(executor, my_blocking_function)

Because otherwise a blocking wait for thread completion is performed at the end of the context. It is sufficient to shut down the executor and allow the threads to wake up and stop in their own time, and not force a wait there which can last several milliseconds.

@kristjanvalur kristjanvalur force-pushed the pool2 branch 2 times, most recently from d79c72e to 9d176df Compare August 16, 2025 17:20
Copy link

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/qasync
  __init__.py
  _windows.py
  tests
  test_qthreadexec.py 117-135, 153, 185-187
Project Total  

This report was generated by python-coverage-comment-action

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.

1 participant