在Python中即时读取更新的文件 [英] Reading updated files on the fly in Python

查看:1509
本文介绍了在Python中即时读取更新的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写两个都解析文件的Python脚本.一个是标准的UNIX日志文件,另一个是二进制文件.我正在尝试找出监视这些数据的最佳方法,以便在数据更新后立即读取它们.到目前为止,我发现的大多数解决方案都是特定于Linux的,但是我需要在FreeBSD中使用它.

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.

显然,一种方法是每隔X倍的时间运行我的脚本,但这似乎很麻烦且效率很低.如果我想让我的Python应用程序在后台连续运行以监视文件并在文件更改/更新后对其进行操作,那么我最好的选择是什么?

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?

推荐答案

您是否尝试过KQueue事件?

Have you tried KQueue events?

http://docs.python.org/library/select.html# kqueue-objects

kqueue是inotify的FreeBSD/OS版本(文件更改通知服务).我没有用过,但是我想这就是您想要的.

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中即时读取更新的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆