Skip to content
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

gh-125500: threading.py Timer context manager #125501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Ginkss
Copy link

@Ginkss Ginkss commented Oct 15, 2024

Add support for context manager. Start on entry, cancel on exit

Add support for context manager. Start on entry, cancel on exit
Copy link

cpython-cla-bot bot commented Oct 15, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Oct 15, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

self.start()

def __exit__(self, exc_type, exc_val, exc_tb):
self.cancel()
Copy link
Contributor

Choose a reason for hiding this comment

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

this should probably join the Timer thread

Copy link
Author

Choose a reason for hiding this comment

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

Why block the main thread for a canceled timer?

Copy link
Contributor

@graingert graingert Oct 16, 2024

Choose a reason for hiding this comment

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

The main thread won't be blocked very long, it's to support use cases like:

with Timer(1, print, ("Trust me, I'm on it...",)):
    time.sleep(2)
os.fork() # all threads need to be joined before calling fork

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

Successfully merging this pull request may close these issues.

2 participants