Skip to main content

Simple, modern file watching and code reload in python.

Project description

watchgod

CI Coverage pypi license

Simple, modern file watching and code reload in python.

(watchgod is inspired by watchdog, hence the name, but tries to fix some of the frustrations I found with watchdog, namely: separate approaches for each OS, an inelegant approach to concurrency using threading, challenges around debouncing changes and bugs which weren't being fixed)

Usage

To watch for changes in a directory:

from watchgod import watch

for changes in watch('./path/to/dir'):
    print(changes)

To run a function and restart it when code changes:

from watchgod import run_process

def foobar(a, b, c):
    ...

run_process('./path/to/dir', foobar, args=(1, 2, 3))

run_process uses PythonWatcher so only changes to python files will prompt a reload, see custom watchers below.

If you need notifications about change events as well as to restart a process you can use the callback argument to pass a function which will be called on every file change with one argument: the set of file changes.

Installation

pip install watchgod

Asynchronous Methods

watchgod comes with an asynchronous equivalents of watch: awatch which uses a ThreadPoolExecutor to iterate over files.

import asyncio
from watchgod import awatch

async def main():
    async for changes in awatch('/path/to/dir'):
        print(changes)

asyncio.run(main())

There's also an asynchronous equivalents of run_process: arun_process which in turn uses awatch:

import asyncio
from watchgod import arun_process

def foobar(a, b, c):
    ...

async def main():
    await arun_process('./path/to/dir', foobar, args=(1, 2, 3))

asyncio.run(main())

arun_process uses PythonWatcher so only changes to python files will prompt a reload, see custom watchers below.

The signature of arun_process is almost identical to run_process except that the optional callback argument must be a coroutine, not a function.

Custom Watchers

watchgod comes with the following watcher classes which can be used via the watcher_cls keyword argument to any of the methods above.

For example:

for changes in watch(directoryin, watcher_cls=RegExpWatcher, watcher_kwargs=dict(re_files=r'^.*(\.mp3)$')):
   print (changes)

For more details, checkout watcher.py, it's pretty simple.

  • AllWatcher The base watcher, all files are checked for changes.

  • DefaultWatcher The watcher used by default by watch and awatch, commonly ignored files like *.swp, *.pyc and *~ are ignored along with directories like .git.

  • PythonWatcher Specific to python files, only *.py, *.pyx and *.pyd files are watched.

  • DefaultDirWatcher Is the base for DefaultWatcher and DefaultDirWatcher. It takes care of ignoring some regular directories.

If these classes aren't sufficient you can define your own watcher, in particular you will want to override should_watch_dir and should_watch_file. Unless you're doing something very odd, you'll want to inherit from DefaultDirWatcher.

Note that events related to directories are not reported (e.g. creation of a directory), but new files in new directories will be reported.

CLI

watchgod also comes with a CLI for running and reloading python code.

Lets say you have foobar.py:

from aiohttp import web

async def handle(request):
    return web.Response(text='testing')

app = web.Application()
app.router.add_get('/', handle)

def main():
    web.run_app(app, port=8000)

You could run this and reload it when any file in the current directory changes with::

watchgod foobar.main

In case you need to ignore certain files or directories, you can use the argument --ignore-paths.

Run watchgod --help for more options. watchgod is also available as a python executable module via python -m watchgod ....

Why no inotify / kqueue / fsevent / winapi support

watchgod (for now) uses file polling rather than the OS's built in file change notifications.

This is not an oversight, it's a decision with the following rationale:

  1. Polling is "fast enough", particularly since PEP 471 introduced fast scandir. For reasonably large projects like the TutorCruncher code base with 850 files and 300k lines of code, watchgod can scan the entire tree in ~24ms. With a scan interval of 400ms that is roughly 5% of one CPU - perfectly acceptable load during development.
  2. The clue is in the title, there are at least 4 different file notification systems to integrate with, most of them not trivial. That is all before we get to changes between different OS versions.
  3. Polling works well when you want to group or "debounce" changes. Let's say you're running a dev server and you change branch in git, 100 files change. Do you want to reload the dev server 100 times or once? Right. Polling periodically will likely group these changes into one event. If you're receiving a stream of events you need to delay execution of the reload when you receive the first event to see if it's part of a group of file changes. This is not trivial.

All that said, I might still use rust's "notify" crate to do the heavy lifting of file watching, see#25.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

watchgod-0.8.1.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

watchgod-0.8.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file watchgod-0.8.1.tar.gz.

File metadata

  • Download URL: watchgod-0.8.1.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for watchgod-0.8.1.tar.gz
Algorithm Hash digest
SHA256 c12d15f3df7d11e740704e45398277f75f1d78f46ad59ca9d7505bfd8b8d3086
MD5 f6ab737432355d244ae4623087203b47
BLAKE2b-256 4de9c6b672b4c5df5e6a880a05476a89da2ef2a62dcbee3763139ff13e15c30e

See more details on using hashes here.

Provenance

File details

Details for the file watchgod-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: watchgod-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for watchgod-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4ba20c2fa3e63df706ab50e694b9453b05395fadb7cbbfd984d71fb1547d485d
MD5 18204f470c0c661d2c4b6f3ec6d83ec0
BLAKE2b-256 6f97cfff3405ecf88a48a6e102141b204463d4b085c0219da6db689ad4221851

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page