在 Python 中结束睡眠循环
End a sleep loop in Python
我写了一个 python 脚本,其中包含一个睡眠循环来监视日志文件,等待新行处理:
file = open('logFile.log', 'r')
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print line, # already has newline
我想对此进行修改,以便在没有换行的情况下花费超过 1 小时时,结束循环并继续执行脚本。到目前为止还没有成功。
将 while 1
(顺便说一句,实际上应该写成 while True
)更改为检查循环中花费了多长时间的条件。
类似于:
file = ...
now = time.time()
while time.time() - now < 1 * 60 * 60:
试试这段代码。
file = open('logFile.log', 'r')
while True:
timeWait = 60*60 # Change the Value to something less to check whether the code works properly or not.
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
timeWait = timeWait - 1
file.seek(where)
if timeWait == 0:
break
else:
print line
您可以更改 while
以检查 1 小时时段并重置 startTime
,如下所示:
file = open('logFile.log', 'r')
startTime = time.time()
while (time.time()-startTime) >= 3600:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
startTime = time.time() #record the last time output was available
print line, # already has newline
只保留一个计数器:
count = 0
file = open('logFile.log', 'r')
while 1:
where = file.tell()
line = file.readline()
if not line:
count = count + 1
if count >= 3600:
break
time.sleep(1)
file.seek(where)
else:
print line, # already has newline
count = 0
# if you get here an hour without any newlines has passed
这个问题的一个重点是1小时没有换行CONTINUOUSLY,所以确实有必要在换行打印后重置时间计数器。
count = 0
one_hour = 60 * 60
file = open('logFile.log', 'r')
while 1:
where = file.tell()
line = file.readline()
if not line:
count = count + 1
if count >= one_hour:
break
time.sleep(1)
file.seek(where)
else:
print line
count = 0 # Here needs to reset the time couter!
我写了一个 python 脚本,其中包含一个睡眠循环来监视日志文件,等待新行处理:
file = open('logFile.log', 'r')
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print line, # already has newline
我想对此进行修改,以便在没有换行的情况下花费超过 1 小时时,结束循环并继续执行脚本。到目前为止还没有成功。
将 while 1
(顺便说一句,实际上应该写成 while True
)更改为检查循环中花费了多长时间的条件。
类似于:
file = ...
now = time.time()
while time.time() - now < 1 * 60 * 60:
试试这段代码。
file = open('logFile.log', 'r')
while True:
timeWait = 60*60 # Change the Value to something less to check whether the code works properly or not.
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
timeWait = timeWait - 1
file.seek(where)
if timeWait == 0:
break
else:
print line
您可以更改 while
以检查 1 小时时段并重置 startTime
,如下所示:
file = open('logFile.log', 'r')
startTime = time.time()
while (time.time()-startTime) >= 3600:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
startTime = time.time() #record the last time output was available
print line, # already has newline
只保留一个计数器:
count = 0
file = open('logFile.log', 'r')
while 1:
where = file.tell()
line = file.readline()
if not line:
count = count + 1
if count >= 3600:
break
time.sleep(1)
file.seek(where)
else:
print line, # already has newline
count = 0
# if you get here an hour without any newlines has passed
这个问题的一个重点是1小时没有换行CONTINUOUSLY,所以确实有必要在换行打印后重置时间计数器。
count = 0
one_hour = 60 * 60
file = open('logFile.log', 'r')
while 1:
where = file.tell()
line = file.readline()
if not line:
count = count + 1
if count >= one_hour:
break
time.sleep(1)
file.seek(where)
else:
print line
count = 0 # Here needs to reset the time couter!