# Keeping an eye on a file where data lines are added. # Uses Python file read. # The file can be a log file, stock markets, electrity spot prices etc. # The generator watch_file keeps reading the last line from file as soon as it appears. # # The rest is for testing. # import numpy as np import time import threading def watch_file(filename,*args,delay=0.1): print('watch file',filename) while True: try: with open(filename,'r') as f: # starting from beginning (0), move file pointer to end of file (2) f.seek(0, 2) while True: line = f.readline() if not line: time.sleep(delay) continue yield line except: pass # no file yet, retry def analyze_data(filename): ave = 0.0 N = 0 for line in watch_file(filename): d = float(line) # or float(line.split()[0]) ave += d N +=1 print(f'got {d:10.5f} average {ave/N:10.5f} number of data {N}') def add_new_data(filename): for _ in range(10): # append data to file with open(filename,'a') as f: f.write(f'{np.random.random():<10.5f}\n') time.sleep(1) if __name__=='__main__': filename = 'random.dat' # with 2 threads: t1 = threading.Thread(target=add_new_data,args=(filename,)).start() t2 = threading.Thread(target=analyze_data,args=(filename,)).start() # feature: t1 writes one line *before* watch_file wakes up in t2 and moves past that line # feature: never stops, has to kill with ctrl-C # # with one thread: #t1 = threading.Thread(target=add_new_data,args=(filename,)).start() #analyze_data(filename) # executes forever # feature: t1 writes one line *before* watch_file wakes up in t2 and moves past that line # feature: never stops, won't die with ctrl-C, has to ctrl-z and kill %