我有一個程序每秒都會寫入一個文件。文件寫入發生在與UI平行的線程中。由於某些硬件問題,它有時會停止寫一天。我想檢查文件是否停止寫入,如果沒有更新,請重新啓動程序。我想檢查文件的時間戳,看看它是否是不會得到更新(和不想去看門狗等怎麼我只是需要某個文件停筆。)監控文件是否停止在python中寫入
try:
if time.time()>(os.stat(filename).st_mtime+2):
raise ValueError("Yikes! Spike")
except ValueError:
with open('errors.log','a') as log:
log.write('Spike occured at '+ time.strftime(
"%H:%M:%S")+' on '+datetime.date.today().strftime('%d/%m/%Y')+'\n')
log.close()
restart_program()
此塊運行的每一秒。但是,這種情況並沒有發生,當應用程序關閉以重新啓動時,它會每秒關閉並且不會再次啓動。我每秒都會收到異常消息。我試圖增加時差,但沒有幫助。
接着我試圖
ftimestamp = os.stat(filename).st_mtime
try:
if os.stat(filename).st_mtime>=ftimestamp:
ftimestamp = time.time()
print "ftimestamp updated and all is well"
else:
ftimestamp = os.stat(filename).st_mtime
raise ValueError("Yikes! Spike!")
print "file time is behind"
except ValueError:
with open('errors.log','a') as log:
log.write('Spike occured at '+ time.strftime(
"%H:%M:%S")+' on '+datetime.date.today().strftime('%d/%m/%Y')+'\n')
log.close()
restart_program()
我試圖更新變量「ftimestamp」到當前時間「了time.time()」,因爲下一個比較只發生之後的一個第二和我想要的文件時要高比以前的時間比較。 (該塊通過wx.CallLater函數每秒運行一次)。
我的程序仍然失敗...我無法理解我要去哪裏錯...有人請幫忙!或者有沒有簡單的檢查文件是否停止寫入的方法?
EOFerror!你有沒有試過 – therealprashant
寫作過程是否每秒刷新它的輸出? – meuh
檢查過程是否仍然存在可能會更簡單。你可以爲此做一個'kill(pid,0)'信號0。 – meuh