是否有更好,更清晰的方式从文本文件中实时传输数据?
目前我正在使用服务器来模拟连续写入的数据,并使用seek()方法只读取最近添加的数据并将其打印到屏幕上。
我所遇到的问题是测试文件是否已被写入,如果它没有在短时间内休眠,那么测试是否已写入 生成并输出以打印到屏幕。
import time
seek1 = 198707
seek2 = 198719
file_open = open('C:\Users\Jay\Desktop\Tools\Uni- Server\UniServerZ\core\\apache2\logs\\access.log')
#C:\Users\Jay\Desktop\Tools\Uni-Server\UniServerZ\core\apache2\logs
#C:\Users\Jay\PycharmProjects\Parsly\\access.log
def generate_list(read_lines):#properly build this with seek positional checks.
#file_object.seek(seek_position)
#lines = file_object.readlines()
for i in read_lines:
yield i
def seek_new_postion(generator):
listt = []
seek = file_open.tell()
old_seek = None
counter = 0
while 1:
if old_seek == seek and not counter == 5:
print "sleeping"
time.sleep(1)
counter +=1
else:
p = generate_list(file_open.readlines())
for i in p:
print i
time.sleep(1)
print "___________________________"
print "start seek: %s" % file_open.tell()
print "old seek: %s" % old_seek
print "___________________________"
old_seek = seek
seek = file_open.tell()
counter = 0
print file_open.tell()
for i in seek_new_postion("test"):
print i