监视目录更改的python dbus方法?

2024-05-16 07:12:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经成功地使用了到dbus mainloop中io_add_watch的python绑定来响应已知单个文件中的更改。但是我现在有一个例子,我运行一个dbus mainloop,需要在目录改变时做一些工作。你知道吗

我已经使用了命令行工具inotifywait -m directory,以及pyinotify提供的一些示例。现在还不清楚的是我是如何把这两者结合起来的。或者我是否应该。我可以启动一个线程,它使用管道直接运行inotifywait,然后写入/run中的一个ram文件,我已经建立了一个io_add_watch来运行它。我对glib/dbus/mainloop还比较陌生,所以它对我来说还是一种魔力。pyinotify对我来说似乎有点沉重,但我在这里没有工作经验。你知道吗

我在Debian Jessie上运行,使用python3。我不是在找跨平台的东西。你知道吗


Tags: 文件工具命令行io目录add示例线程
2条回答

PyInotify可以轻松地监视目录:

notifier = pyinotify.Notifier(wm, handler)
wm.add_watch('/tmp', pyinotify.IN_CREATE)
notifier.loop()

完整的教程在这里:https://github.com/seb-m/pyinotify/wiki/Tutorial#1-using-the-notifier-class-without-timeout

特别是要用dbus循环编织notify内容,技巧是使用来自pyinotifyThreadedNotifier。我用了这样的方法:

watchManager = pyinotify.WatchManager()
inotifier = pyinotify.ThreadedNotifier(watchManager, FileEventHandler(mainService.eventStream))
inotifier.start()
eventsPath = Path('/Pilot/Schedules')
if not eventsPath.exists():
    eventsPath.mkdir()
watchManager.add_watch(eventsPath.as_posix(), pyinotify.IN_CLOSE_WRITE | pyinotify.IN_DELETE, rec=True, auto_add=True)

mainloop = glib.MainLoop()
try:
    mainloop.run()
except KeyboardInterrupt:
    mainloop.quit()
    inotifier.stop()

我的FileEventHandler使用了process_IN_CLOSE_WRITE等标准方法来提交dbus更改。你知道吗

相关问题 更多 >