No project description provided
Project description
py_volume_watcher
py_volume_watcher is a lightweight Python library that lets you monitor a folder or volume (e.g., a mounted Docker volume) for new or changed files — only after they are fully written and stable.
This is especially useful in containerized environments, where file updates might be streamed or take time to finish. Instead of reacting to partial file writes, py_volume_watcher waits until the file's content hash (sha256) stops changing before emitting an event.
🔧 Features
- Detects new or modified files
- Ignores unchanged files
- Polling-based (no inotify), so it works across platforms (including Docker volumes)
- Waits for file stability before triggering an event
- Safe shutdown with context manager or manual control
📦 Installation
pip install py_volume_watcher
(You can also copy the code directly if it's not published on PyPI yet.)
🐍 Requirements
-
Python 3.9+
The code uses modern typing features likestr | PathandQueue[Path], which require Python 3.9 or newer. -
No external dependencies
py_volume_watcherrelies only on the Python standard library:hashlibpathlibloggingthreadingqueuetime
This makes it lightweight and portable — perfect for containerized or minimal environments.
🧠 How it Works
Every polling_interval_sec (default: 0.5s), the watcher:
- Scans the directory using a glob pattern (e.g.,
*.json) - Computes the SHA256 hash of each file
- If a new file appears or its hash changes, it's added to the event queue
- You can consume these events using a simple iterator interface
✅ Recommended Usage (Context Manager)
from py_volume_watcher import FileWatcher
with FileWatcher("/path/to/volume", pattern="*.json") as watcher:
for file in watcher:
print(f"Detected stable file: {file}")
# You can break safely; the thread will stop properly
if file.name == "stop.json":
break
- ✅ Thread starts automatically on
__enter__() - ✅ Thread stops gracefully on
__exit__()— even onCtrl+C,break, or exceptions
❌ Incorrect Usage (Without Context Manager)
watcher = FileWatcher("/path/to/volume", pattern="*.json")
for file in watcher:
print(file)
break # ❌ This breaks the loop but leaves the thread running in background
If you must manage lifecycle manually, call __enter__() and __exit__() yourself:
watcher = FileWatcher("/path/to/volume", pattern="*.json")
watcher.__enter__()
try:
for file in watcher:
print(file)
break
finally:
watcher.__exit__(None, None, None)
📂 Typical Use Case in Docker
Mount a volume and use the watcher to wait for "complete" files:
docker run -v $(pwd)/input:/data your-image
In Python:
from py_volume_watcher import FileWatcher
with FileWatcher("/data", pattern="*.csv") as watcher:
for file in watcher:
print(f"✅ Ready to process: {file}")
This avoids processing files that are still being copied into the container.
⚙️ Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
base |
str | Path |
required | Directory to watch |
pattern |
str |
"*" |
Glob pattern to match files |
polling_interval_sec |
float | int |
0.5 |
Interval in seconds between scans |
on_exit_timeout |
float | int |
1.0 |
Max time to wait for thread to join |
📄 License
MIT License — free to use and modify.
🧪 Tips
- Use longer polling intervals if files are large or written slowly.
- Combine with file naming conventions (e.g., temp + rename) for extra safety.
- Logging is available via the
py_volume_watcherlogger.
Happy watching! 🔍
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_volume_watcher-0.1.0.tar.gz.
File metadata
- Download URL: py_volume_watcher-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.11 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f65d7b572d7f61ff1ddebe3ca9d2b71e2612cce0bf84c89b48c6f1331bea60f0
|
|
| MD5 |
1660700fef7b2ac65e4d80ed5ab8b9e4
|
|
| BLAKE2b-256 |
9b15018953b88e6e56a74f0aaea079ad1b1015a957352366533d4637b852a4db
|
File details
Details for the file py_volume_watcher-0.1.0-py3-none-any.whl.
File metadata
- Download URL: py_volume_watcher-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.10.11 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85c5a987e5d7ef79c5be2997346eed93eab4ad01cf47fce3e1aa5a8960758779
|
|
| MD5 |
97d5b2bfd0e3948b520100b0565f2a02
|
|
| BLAKE2b-256 |
5c7c3e364cc46b3ed0c3c1f11705da84532493d0bee43899d6900b0e70a384b7
|