读取文件时的同步问题

时间:2018-10-22 11:30:40

标签: python linux

我有两个不同的过程。我们称它们为A和B。
A(用C编写)将数据写入文本文件(dump.txt)。一旦有printf,它将刷新数据。 和B(用Python3编写)在指定的时间戳下定期从文本文件中读取数据。

我使用B读取它的方式如下:

import os
import time
import subprocess

ctime = time.time() 

tail_lat = "cat " + "dump.txt" + " | egrep '" + str(int(ctime)) + "' | cut -d \, -f 2,10"
print("last modified: {}, curr_time={}".format(time.ctime(os.path.getmtime(self.input_file)), ctime))

maxRetries = 3
retries = 0
while retries < maxRetries:
    process = subprocess.Popen(tail_lat, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out, err = process.communicate()
    self.logger.info('ctime={} out = {}, retries={}'.format(ctime, out.decode(), retries))
    try:
        values = [float(x) for x in out.decode().split("\n")[:-1][0].split(",")]
        load = values[0]
        latency = values[1]
        break
    except IndexError:
        time.sleep(0.01)
        retries+=1

if retries == 3:
    load = 0
    latency = 0

可以看出,存在一些同步问题,由于这些原因,我无法从某些行读取数据

last modified: Mon Oct 22 13:23:30 2018, curr_time=1540207411
load=0, latency=0
last modified: Mon Oct 22 13:23:32 2018, curr_time=1540207412
load=99833.900166, latency=0.2381
last modified: Mon Oct 22 13:23:33 2018, curr_time=1540207413
load=100713.899286, latency=0.2631
last modified: Mon Oct 22 13:23:34 2018, curr_time=1540207414
load=100346.899653, latency=0.2251
last modified: Mon Oct 22 13:23:35 2018, curr_time=1540207415
load=100268.899731, latency=0.2301
last modified: Mon Oct 22 13:23:36 2018, curr_time=1540207416
load=100298.899701, latency=0.4261
last modified: Mon Oct 22 13:23:37 2018, curr_time=1540207418
load=0, latency=0
last modified: Mon Oct 22 13:23:38 2018, curr_time=1540207419
load=0, latency=0
last modified: Mon Oct 22 13:23:39 2018, curr_time=1540207420
load=0, latency=0
last modified: Mon Oct 22 13:23:41 2018, curr_time=1540207421
load=98935.901064, latency=138.1

是否可以使用Python中的生成器异步读取? 我当时在看this,但无法找到一种方法来检查时间戳记(ctime)是否在产生一行之前匹配。

任何建议都会有所帮助。

0 个答案:

没有答案