在 Python 中动态读取更新的文件
我正在编写两个都解析文件的 Python 脚本。一个是标准的 unix 日志文件,另一个是二进制文件。我正在尝试找出监控这些数据的最佳方法,以便我可以在数据更新后立即读取数据。到目前为止,我发现的大多数解决方案都是特定于 Linux 的,但我需要它在 FreeBSD 中工作。
显然,一种方法是每隔 X 时间运行我的脚本,但这看起来很粗鲁且效率低下。如果我希望我的 Python 应用程序在后台持续运行,监视文件并在文件更改/更新后对其进行操作,我最好的选择是什么?
I'm writing two Python scripts that both parse files. One is a standard unix logfile and the other is a binary file. I'm trying to figure out the best way to monitor these so I can read data as soon as they're updated. Most of the solutions I've found thus far are linux specific, but I need this to work in FreeBSD.
Obviously one approach would be to just run my script every X amount of time, but this seems gross and inefficient. If I want my Python app running continuously in the background monitoring a file and acting on it once it's changed/updated, what's my best bet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试过 KQueue 事件吗?
http://docs.python.org/library/select.html#kqueue-objects
kqueue 是 FreeBSD/OS 版本的 inotify(文件更改通知服务)。我没用过这个,但我想这就是你想要的。
Have you tried KQueue events?
http://docs.python.org/library/select.html#kqueue-objects
kqueue is the FreeBSD / OS version of inotify (file change notification service). I haven't used this, but I think it's what you want.
我曾经为用 Python 构建的解析器创建了一种守护进程。我需要观看一系列文件并用 Python 处理它们,它必须是一个真正的多操作系统解决方案(在本例中为 Windows 和 Linux)。我编写了一个程序,通过检查文件的修改时间来监视文件列表。程序休眠一段时间,然后检查正在观看的文件的修改时间。如果修改时间比之前注册的时间新,则文件已更改,因此必须对此文件进行处理。
类似这样的:
它看起来不像顶级代码,但它运行得很好。
还有其他与此相关的问题,但我不知道他们是否会直接回答您的问题:
如何实现 tail -F 的 pythonic 等效项?
如何监视文件的更改?
我如何“监视”文件的修改/更改?
希望这会有所帮助!
I once did to make a sort of daemon process for a parser built in Python. I needed to watch a series of files and process them in Python, and it had to be a truly multi-OS solution (Windows & Linux in this case). I wrote a program that watches over a list of files by checking their modification time. The program sleeps for a while and then checks the modification time of the files being watched. If the modification time is newer than the one previously registered, then the file has changed and so stuff has to be done with this file.
Something like this:
It does not look like top notch code, but it works quite well.
There are other SO questions about this, but I don't know if they'll provide a direct answer to your question:
How to implement a pythonic equivalent of tail -F?
How do I watch a file for changes?
How can I "watch" a file for modification / change?
Hope this helps!