python读文件一次读特定行_如何在python中一次读写同一个文件

有三个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循环中连续运行。

由于这个原因,同一个文件被三个程序同时访问。

寻找想法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值