如何仅在使用 python 更新后才从输入文件中读取文本
实际上,我会将 application1 中的输入提供给 input.txt,该实习生会触发 {used pyinotify} program1 来运行此 program1,该程序会更新 output.txt 文件,但正在从 output.txt 读取的 application1 不会等待 program1 完成在文本文件(output.txt)上写入过程,它从output.txt中读取旧数据。我需要 Application1 等待 program1 进程完成,我该怎么做?
import pyinotify,subprocess
def onChange(ev):
cmd = ['/usr/bin/env', 'python','/var/www/cgi-bin/version2_1.py', ev.pathname]
subprocess.Popen(cmd).communicate()
wm = pyinotify.WatchManager()
wm.add_watch('/var/www/cgi-bin/input.txt', pyinotify.IN_CLOSE_WRITE, onChange)
notifier = pyinotify.Notifier(wm)
notifier.loop()
这是我用来在触发输入文本时在后台运行我的 python 程序1的程序,在此触发之后,application1 立即执行此语句 out_file=open("/var/www/cgi-bin/output. txt", "r").read()
现在 application1 在程序 1 更新之前获取输出内容!我希望 Aplication1 等待 program1 完成其运行并通过 output.txt 进行更新,
请告诉我如何解决此问题。
谢谢:)
Actually i will give input from my application1 to input.txt, which intern triggers {used pyinotify} program1 to run this program1 which updates output.txt file, but application1 which is reading from output.txt doesn't wait for program1 to complete the writing process on the text file(output.txt), It reads the old data from output.txt. I need Application1 to wait for the completion of program1's process how can i do this??
import pyinotify,subprocess
def onChange(ev):
cmd = ['/usr/bin/env', 'python','/var/www/cgi-bin/version2_1.py', ev.pathname]
subprocess.Popen(cmd).communicate()
wm = pyinotify.WatchManager()
wm.add_watch('/var/www/cgi-bin/input.txt', pyinotify.IN_CLOSE_WRITE, onChange)
notifier = pyinotify.Notifier(wm)
notifier.loop()
This is the program that i have used to run my python program1 on the background on trigger to input text, right after this trigger the application1's execute this statement out_file=open("/var/www/cgi-bin/output.txt", "r").read()
Now application1 takes content of output before it's updated by program1!! I want Aplication1 to wait for the program1 to complete its run and updatition over output.txt
pls give me an idea how can i go about this..
Thank you :)
在 Python 中观察文件更改的方法有多种,其中大多数都是特定于平台的。由于您没有指定平台,因此这里有一个跨平台包:
http://packages.python.org/watchdog/
您还应该看看答案对于这个问题:
如何监视文件的更改?
摘自 inotify 常见问题解答:
问:IN_MODIFY 和 IN_CLOSE_WRITE 之间有什么区别?
IN_MODIFY 事件在文件内容更改时发出(例如通过 write() 系统调用),而 IN_CLOSE_WRITE 在关闭更改的文件时发生。这意味着每个更改操作都会导致一个 IN_MODIFY 事件(在操作打开的文件期间可能会发生多次),而 IN_CLOSE_WRITE 仅发出一次(在关闭文件时)。
问:使用 IN_MODIFY 还是 IN_CLOSE_WRITE 更好?
具体情况因情况而异。通常使用 IN_CLOSE_WRITE 更合适,因为如果发出,相应文件上的所有更改都会安全地写入文件内。 IN_MODIFY 事件不一定意味着文件更改已完成(数据可能保留在应用程序的内存缓冲区中)。另一方面,必须使用 IN_MODIFY 监视许多日志和类似文件 - 在这些文件永久打开的情况下,因此无法发出 IN_CLOSE_WRITE。
您确定正在观察 IN_CLOSE_WRITE 吗?如果您观察 IN_MODIFY,那么如果您在写入数据刷新到文件之前收到该事件,我不会感到惊讶。
There are several ways to watch file changes in Python, most of them are platform-specific. Since you didn't specify your platform, here's a cross-platform package:
http://packages.python.org/watchdog/
You should also take a look at the answers for this question:
How do I watch a file for changes?
Edit
From the inotify FAQ:
Q: What is the difference between IN_MODIFY and IN_CLOSE_WRITE?
The IN_MODIFY event is emitted on a file content change (e.g. via the write() syscall) while IN_CLOSE_WRITE occurs on closing the changed file. It means each change operation causes one IN_MODIFY event (it may occur many times during manipulations with an open file) whereas IN_CLOSE_WRITE is emitted only once (on closing the file).
Q: Is it better to use IN_MODIFY or IN_CLOSE_WRITE?
It varies from case to case. Usually it is more suitable to use IN_CLOSE_WRITE because if emitted the all changes on the appropriate file are safely written inside the file. The IN_MODIFY event needn't mean that a file change is finished (data may remain in memory buffers in the application). On the other hand, many logs and similar files must be monitored using IN_MODIFY - in such cases where these files are permanently open and thus no IN_CLOSE_WRITE can be emitted.
Are you sure you're observing IN_CLOSE_WRITE? If you observe IN_MODIFY, then I won't be surprised if you get the event before the written data is flushed to the file.