Asynchronous library to watch files in real time.
Project description
Library to "watch" files in a directory and call a
callback function (filename, lines)
every time one of the monitored files is recorded, in real time.
In practical terms, this can be compared to UNIX's tail -F * .log
command,
but instead of having lines printed in stdout, a Python function is called.
Like tail, it is in charge of "watching" new files that are created after startup and "unlock" those that are removed in the meantime. This means that you will be able to "follow" and support rotating log files as well.
Key Features
- Uses Asyncio for asynchronous reading and monitoring.
- The implementation chooses automatically depending on the compatibility of the system.
- Monitoring of several files in the same directory or just one.
- Asynchronous callback function.
Getting started
All code samples require Python 3.6+.
Basic Usage
import asyncio
from aiowatcher import AIOWatcher
async def callback(filename, line):
print(line)
async def main():
lw = AIOWatcher('var', callback, extensions=['txt'])
await lw.init()
await lw.loop()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Non blocking
import asyncio
from aiowatcher import AIOWatcher
async def callback(filename, line):
print(line)
async def main():
lw = AIOWatcher('var', callback, extensions=['txt'])
while True:
await lw.loop(blocking=False)
await asyncio.sleep(0.1)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Tail
import asyncio
from aiowatcher import AIOWatcher
async def callback(filename, lines):
for line in lines:
print(line[:-1])
async def main():
lw = AIOWatcher('var', callback, extensions=['txt'])
await lw.tail(3)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
License
aiowatcher
is offered under the Apache 2 license.
Source code
The latest version of the developer is available on a GitHub repository: https://github.com/py-paulo/aiowatcher.git
Changelog
0.0.5 (22-09-2020)
Features
- new
AIOWatcher().tail
function to read N lines from a file and return as list.
Bugfixes
AIOWatcher.readlines()
returns a list instead of a string.
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
Built Distribution
File details
Details for the file aiowatcher-0.0.5.tar.gz
.
File metadata
- Download URL: aiowatcher-0.0.5.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 913e0fa9b7380935a37aa31fb2ea87f4f38ed41d39cd818e8dcefc22b707ba69 |
|
MD5 | acb632e16e519e477cf3e8eb1fdfe0f1 |
|
BLAKE2b-256 | 5ed9d531a214c3087e39b53fd331d5623595e90f2774feac561af459b2eda57c |
File details
Details for the file aiowatcher-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: aiowatcher-0.0.5-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c9ca961daf12812c4fb07eb0f8c2f512862b3b2c35352c6cd8ef71adfb38ad6 |
|
MD5 | abf2fa7f914f47fc49e9cea26d01ea64 |
|
BLAKE2b-256 | 320f6c64bb8deeff56f699d032a598665c965550b1968d76bea305630ccbcd53 |