Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,12 @@ def run(self):
self.function(*self.args, **self.kwargs)
self.finished.set()

def __enter__(self):
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



# Special thread class to represent the main thread

Expand Down
Loading