Skip to main content

Simple, modern file watching and code reload in python.

Project description

BuildStatus Coverage pypi

Simple, modern file watching and code reload in python.

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 will will be called on every file change with one argument: the set of file changes.

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)

loop = asyncio.get_event_loop()
loop.run_until_complete(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))

loop = asyncio.get_event_loop()
loop.run_until_complete(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 callback argument if provide 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 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 and 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.

CLI

wathgod 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

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.

    With a reasonably large project 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’s 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. And that’s 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 whole bunch of file changes, this is not completely trivial.

All that said, I might still implement inotify support. I don’t use anything other than Linux so I definitely won’t be working on dedicated support for any other OS.

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.5.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

watchgod-0.5-py35.py36.py37-none-any.whl (10.1 kB view details)

Uploaded Python 3.5 Python 3.6 Python 3.7

File details

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

File metadata

  • Download URL: watchgod-0.5.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.7

File hashes

Hashes for watchgod-0.5.tar.gz
Algorithm Hash digest
SHA256 78d44cbacf4fbbf7573d10fcf0f156f1b0d71598d32477c34f8cf66001bafc1f
MD5 54ac825669108d66ead8d7a893bad837
BLAKE2b-256 c3995f6830868033454b526fd51800a044850c3942c9d4965183da22900d3244

See more details on using hashes here.

Provenance

File details

Details for the file watchgod-0.5-py35.py36.py37-none-any.whl.

File metadata

  • Download URL: watchgod-0.5-py35.py36.py37-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3.5, Python 3.6, Python 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.7

File hashes

Hashes for watchgod-0.5-py35.py36.py37-none-any.whl
Algorithm Hash digest
SHA256 30ffb90c692e0797d23a39d695928c57d8c01a4a9708434e9dca16d7bd4e24c7
MD5 ded13bf6896edb10933771c8909fb22c
BLAKE2b-256 5ca2a3c50820206ad9771a22e1f06dc1a212a5cc3ea8acc14a3953465d6b9bad

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