Skip to content

Conversation

@Prosperibe12
Copy link
Contributor

Fixes #1090

Adds context manager support to Observer to ensure
correct startup and teardown semantics.

Eg 1
class MyEventHandler(FileSystemEventHandler):
def on_any_event(self, event: FileSystemEvent) -> None:
print(event)

event_handler = MyEventHandler()
observer = Observer()
observer.schedule(event_handler, ".", recursive=True)

with observer:
while True:
time.sleep(1)

Eg 2
if name == "main":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()

with observer:
    while True:
        time.sleep(1)

@Prosperibe12
Copy link
Contributor Author

@BoboTiG kindly review the PR to the proposed improvement

@BoboTiG
Copy link
Collaborator

BoboTiG commented Jan 5, 2026

Thanks @Prosperibe12 !

That seems good for a first quick look.

Can you add a line in the changelog and fix tests + linters?

@Prosperibe12
Copy link
Contributor Author

@BoboTiG I have made few changes to fix the tests, linters and updated the changelog.

changelog.rst Outdated

**Other Changes**

- [core] Add context manager support to ``Observer`` class. The observer can now be used with a ``with`` statement for automatic start/stop management. (`#XXXX <https://github.com/gorakhargosh/watchdog/pull/1149>`__)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- [core] Add context manager support to ``Observer`` class. The observer can now be used with a ``with`` statement for automatic start/stop management. (`#XXXX <https://github.com/gorakhargosh/watchdog/pull/1149>`__)
- [core] Add context manager support to ``Observer`` class. The observer can now be used with a ``with`` statement for automatic start/stop management. (`#1090 <https://github.com/gorakhargosh/watchdog/pull/1149>`__)

tests/utils.py Outdated
Provides some robustness for the otherwise flaky nature of asynchronous notifications.
"""
assert self.event_queue.get(timeout=timeout)[0] == expected_event
try:
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can revert this change.

@BoboTiG
Copy link
Collaborator

BoboTiG commented Jan 7, 2026

This is a little bit hard to review.

I would like a PR with only Observer-related changes, without changes in tests except for the new one(s) testing the context manager (do not alter other tests, this is not the purpose of such a PR, even if they are flaky). Can you do that?

@Prosperibe12
Copy link
Contributor Author

ok

@Prosperibe12 Prosperibe12 marked this pull request as draft January 7, 2026 10:23
@Prosperibe12 Prosperibe12 force-pushed the issue-1090-allow-observer-as-context-manager branch from 6e63504 to 6699a6f Compare January 7, 2026 11:45
@Prosperibe12 Prosperibe12 marked this pull request as ready for review January 7, 2026 12:36
@Prosperibe12
Copy link
Contributor Author

@BoboTiG
reverted back to only observer related changes. some tests are failing, but they also fail on master branch, these appear to be pre-existing flaky tests unrelated to my changes. The new context manager tests in tests/test_observer.py
all pass consistently.

@BoboTiG BoboTiG merged commit f13abdd into gorakhargosh:master Jan 7, 2026
22 of 30 checks passed
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.

Allow to use Observers as a context manager

3 participants