我寫了一個無限期運行的Python腳本。它使用PyInotify
監視目錄,並使用Multiprocessing
模塊通過外部腳本運行在這些目錄中創建的任何新文件。這一切都很好。Python服務 - 用時間戳寫入文件名
我遇到的問題是將輸出寫入文件。我選擇的文件名使用當前日期(使用datetime.now
),理論上應該每小時滾動一小時。
now = datetime.now()
filename = "/data/db/meta/%s-%s-%s-%s.gz" % (now.year, now.month, now.day, now.hour)
with gzip.open(filename, 'ab') as f:
f.write(json.dumps(data) + "\n")
f.close() #Unsure if I need this, here for debug
不幸的是,當小時滾動時 - 輸出停止並且從不返回。沒有例外,它只是停止工作。
total 2.4M
drwxrwxr-x 2 root root 4.0K Sep 8 08:01 .
drwxrwxr-x 4 root root 12K Aug 29 16:04 ..
-rw-r--r-- 1 root root 446K Aug 29 16:59 2016-8-29-16.gz
-rw-r--r-- 1 root root 533K Aug 30 08:59 2016-8-30-8.gz
-rw-r--r-- 1 root root 38K Sep 7 10:59 2016-9-7-10.gz
-rw-r--r-- 1 root root 95K Sep 7 14:59 2016-9-7-14.gz
-rw-r--r-- 1 root root 292K Sep 7 15:59 2016-9-7-15.gz #Manually run
-rw-r--r-- 1 root root 834K Sep 8 08:59 2016-9-8-8.gz
這些文件是不是真的屬於root,只是改變了他們對公共消費
正如你所看到的,所有的時間標記結束的文件:59接下來的一小時從未發生。
在做這件事情時,我是否應該考慮一些事情?有沒有我缺少的無限期運行Python腳本?
經過一番窺視。 PyInotify似乎是我的問題。 這裏(https://unix.stackexchange.com/questions/164794/why-doesnt-inotifywatch-detect-changes-on-added-files)
我懷疑你是對的。它可能與PyInotify代碼本身有關。我將使用你的代碼來幫助調試。 – gleb1783
看到更新 - 你是對的。 – gleb1783
很高興聽到您破解它 - 感謝您的更新! –