文件轮询(重读) [英] File Polling (Rereading)
问题描述
你好Python程序员,
我有以下问题:
我想每10秒读取一个文件的内容。那现在最好的功能是什么?b
最好的功能呢?打开文件,阅读并关闭它
消耗大量的CPU时间。是否有更好的解决方案?
问候Daniel
-
请使用我的GnuPG(PGP)公钥进行安全保护消息交换。
( http://157.161.155.222/ damueller-pubkey.txt )
Hello Fellow Python Programmers,
I have the following problem:
i want to read the content of a file every 10 seconds. now what is the
best funktion to do that? to open the file, read it and close it
consumes a lot of CPU time. is there a better solution?
Greetings Daniel
--
Please use my GnuPG (PGP) Public-Key for secure message exchange.
(http://157.161.155.222/damueller-pubkey.txt)
推荐答案
Daniel Mueller写道:
Daniel Mueller wrote:
你好Python程序员,
我有以下问题:
我想每10秒读取一个文件的内容。那现在做什么最好的功能呢?打开文件,读取并关闭它消耗了大量的CPU时间。是否有更好的解决方案?
Hello Fellow Python Programmers,
I have the following problem:
i want to read the content of a file every 10 seconds. now what is the
best funktion to do that? to open the file, read it and close it
consumes a lot of CPU time. is there a better solution?
否。至少不是如果您真正想要的是
文件的全部内容。如果你只对内容感兴趣,如果文件实际上已经改变了 - 这是一个完全不同的动物,并且使用
stat-calls(在某处)使用unix在os模块中)。我不确定将多少钱延伸到windows,但我非常有信心有类似的东西
可用。
Diez-
No. At least not if what you really want is always the full content of the
file. If you''re only interested in the content if the file has actually
changed - thats a totally different animal, and is under unix doable using
stat-calls (somewhere in the os module). I''m not sure how much that extends
to windows, but I''m pretty much confident that there is similar stuff
available.
Diez-
Diez B. Roggisch写道:
Diez B. Roggisch wrote:
如果你只对内容,如果文件实际上已经改变 - 这是一个完全不同的动物,
很高兴听到!我只需要改变!你有代码示例吗?
并且使用
stat-calls(在os模块中的某个地方)使用unix。我不确定将多少延伸到窗户,但我非常有信心有类似的东西可用。
If you''re only interested in the content if the file has actually
changed - thats a totally different animal,
Good to hear! i only need the changes! do you have code examples?
and is under unix doable using
stat-calls (somewhere in the os module). I''m not sure how much that extends
to windows, but I''m pretty much confident that there is similar stuff
available.
im在Linux下编程
-
请使用我的GnuPG(PGP)公钥进行安全的消息交换。
( http://157.161.155.222/damueller-pubkey.txt )
>很高兴听到!我只需要改变!你有代码示例吗?
这是一个误解:我并不是指改变。你能得到什么
叫
os.stat
是最后修改的时间戳 - 所以如果在您上次阅读
文件后没有修改,则可以跳过重读
文件。
但你_不能只读取文件中已更改的内容 - 在python中没有
这样的东西,也不是底层的OS(这就是原因
对于python没有这样的功能)
我建议你告诉我们更多关于你真正想要完成的事情,然后
我们可以提供更好的服务建议。
Diez
> Good to hear! i only need the changes! do you have code examples?
This is a misunderstanding: I didn''t mean "the changes". What you can get by
calling
os.stat
is the timestamp of the last modification - so you can then skip rereading
the file if there has been no modification after the last time you read the
file.
But you _can''t_ only read what has been changed in the file - there is no
such thing neither in python, nor the underlying OSses (which is the reason
for python not having such a functionality)
I suggest you tell us more about what you actually want to accomplish, then
we might be able to offer better advice.
Diez
这篇关于文件轮询(重读)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!