-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
base: main
Are you sure you want to change the base?
Conversation
Add support for context manager. Start on entry, cancel on exit
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 |
self.start() | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.cancel() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
Add support for context manager. Start on entry, cancel on exit