Watchgod

0.8.2 · renamed · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to use `watchgod.watch` to detect file system changes in a specified directory. It creates a temporary directory and file, then watches it for a short period. For real-world use, you would typically run the `watch` loop indefinitely.

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()

view raw JSON →