Watchgod
Watchgod is a Python library designed for simple file watching and code reloading. It was last released as version 0.8.2 in April 2022. This package is now considered inactive and has been superseded by `watchfiles`, which offers a more performant Rust-based backend for file system notifications. `watchgod` relies on file polling for change detection.
Warnings
- breaking This package (watchgod) has been renamed to `watchfiles` and is no longer actively developed. The `watchgod` package will only receive critical security fixes. Users are strongly encouraged to migrate to `watchfiles` for new projects and to update existing `watchgod` installations.
- gotcha `watchgod` (version 0.8.2 and earlier) uses file polling to detect changes, which can be less efficient and may introduce a slight delay compared to OS-native file system notifications.
- deprecated The `Development Status :: 7 - Inactive` classifier has been added to the `watchgod` PyPI project, indicating that the project is no longer under active development.
Install
-
pip install watchgod
Imports
- watch
from watchgod import watch
- awatch
from watchgod import awatch
- run_process
from watchgod import run_process
- arun_process
from watchgod import arun_process
Quickstart
import time
from watchgod import watch
import os
def main():
# Create a dummy directory and file for demonstration
if not os.path.exists('watched_dir'):
os.makedirs('watched_dir')
with open('watched_dir/test.txt', 'w') as f:
f.write('initial content')
print("Watching './watched_dir' for changes (will run for 5 seconds)...")
# The 'watch' function yields sets of changed files
# In a real application, you'd typically run this indefinitely
# or within a controlled loop.
# For this quickstart, we'll simulate a short watch period.
# Note: watchgod uses polling, so changes might not be immediate.
start_time = time.time()
for changes in watch('./watched_dir'):
print(f"Detected changes: {changes}")
if time.time() - start_time > 5: # Limit execution time for quickstart
break
print("Quickstart demonstration complete.")
if __name__ == '__main__':
main()