跳转到主要内容

inotify for asyncio

项目描述

inotipy 是 Linux
[`inotify(7)`](http://man7.org/linux/man-pages/man7/inotify.7.html) API 的高级 Python 包装器,
旨在与
[`asyncio`](https://docs.pythonlang.cn/3/library/asyncio.html) 事件循环一起使用
在 Python 3.5 或更高版本中。这使得您可以高效地监视一个或多个
文件或目录的有趣事件,而无需
不断轮询文件系统。但是请注意,如果事件
太多太快,内核可能会开始丢弃
它们,这将通过设置 `IN.Q_OVERFLOW`
位的事件通知给您。


基本用法
===========

假设有一个目录,最初为空,名为“test”。
以下脚本监视该目录以进行更改

import sys
import asyncio
import inotipy

loop = asyncio.get_event_loop()

paths_to_watch = ["test"] # 根据需要编辑

async def mainline()
watcher = inotipy.Watcher.create()
for path in paths_to_watch
watcher.watch(path, inotipy.IN.ALL_EVENTS)
#end for
while True
event = await watcher.get()
sys.stdout.write("收到事件: %s\n" % repr(event))
#end while
#end mainline

loop.run_until_complete(mainline())

假设我们启动脚本,然后在另一个终端会话中
执行以下一系列命令行操作

$ echo hi >test/f1
$ mv test/f1 test/f2
$ cat test/f2
$ rm test/f2
$ rmdir test

然后我们应该看到脚本报告一个类似的事件流
这个

收到事件:Event(1, [<EVENT_BIT.CREATE: 8>], 0, 'f1')
收到事件:Event(1, [<EVENT_BIT.OPEN: 5>], 0, 'f1')
收到事件:Event(1, [<EVENT_BIT.MODIFY: 1>], 0, 'f1')
收到事件:Event(1, [<EVENT_BIT.CLOSE_WRITE: 3>], 0, 'f1')
收到事件:Event(1, [<EVENT_BIT.MOVED_FROM: 6>], 217185, 'f1')
收到事件:Event(1, [<EVENT_BIT.MOVED_TO: 7>], 217185, 'f2')
收到事件:Event(1, [<EVENT_BIT.OPEN: 5>], 0, 'f2')
收到事件:Event(1, [<EVENT_BIT.ACCESS: 0>], 0, 'f2')
收到事件:Event(1, [<EVENT_BIT.CLOSE_NOWRITE: 4>], 0, 'f2')
收到事件:Event(1, [<EVENT_BIT.DELETE: 9>], 0, 'f2')
收到事件:Event(1, [<EVENT_BIT.DELETE_SELF: 10>], 0, '')
收到事件:Event(1, [<EVENT_BIT.IGNORED: 15>], 0, '')

这应该给你一个基本的味道。对于更详细的例子,
请参阅我的 `inotipy_examples` 仓库
([GitLab](https://gitlab.com/ldo/inotipy_examples),
[GitHub](https://github.com/ldo/inotipy_examples)).

还有更多功能可用:查看 `inotify(7)` 手册页
以获取更多详细信息,当然还有 `inotify.py` 自身的文档。


劳伦斯·德·奥利维罗 <ldo@geek-central.gen.nz>
2018年3月11日


项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。

源代码分发

此版本没有源代码分发文件。请参阅 生成分发存档的教程

构建分发

inotipy-0.1.1-py3-none-any.whl (16.0 kB 查看哈希值)

上传时间 Python 3

支持者:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面