有三个python程序,writer程序
(writer.py)
写入文件
output.txt
(reader_1.py, reader_2.py)
从同一个
output.txt file
同时。
如果另一个程序正在写入输出文件,如何避免读取器程序读取?
如何在python中高效地处理单写器和多读器问题?
我试图实现fnctl锁定机制,但是在python中找不到这个模块。
作家.py
#!/usr/bin/python
import subprocess
import time
cycle = 10
cmd="ls -lrt"
def poll():
with open("/home/output.txt", 'a') as fobj:
fobj.seek(0)
fobj.truncate()
try:
subprocess.Popen(cmd, shell=True, stdout=fobj)
except Exception:
print "Exception Occured"
# Poll the Data
def do_poll():
count = int(time.time())
while True:
looptime = int(time.time())
if (looptime - count) >= cycle:
count = int(time.time())
print('Begin polling cycle')
poll()
print('End polling cycle')
def main():
do_poll()
if __name__ == "__main__":
main()
#!/usr/bin/python
with open("/home/output10.txt", 'r') as fobj:
f=fobj.read()
print f
#!/usr/bin/python语言
以open(“/home/output10.txt”,'r')作为fobj:
f=fobj.read()
打印f
reader_1.py和reader_2.py在while循环中连续运行。
由于这个原因,同一个文件被三个程序同时访问。
寻找想法。