I am creating a Python program using Watchdog that tracks a set of files and takes action based on changes. I put the exact example from their site into a file:
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
Then I noticed something strange. I also set a watchdog for both Python 2 and Python 3 (using pip2 install watchdogand pip3 install watchdog) and at the same time. However, when I run the program in Python 2 and 3 and do the same modification once for each, this happens:
$ python2 watch_test.py
2015-09-30 11:18:32 - Modified file: ./watch_test.py
$ python3 watch_test.py
2015-09-30 11:18:39 - Modified file: ./watch_test.py
2015-09-30 11:18:39 - Modified file: ./watch_test.py
2015-09-30 11:18:39 - Modified file: ./watch_test.py
What interests me is what can cause this behavior and how can I fix it.
This question is not a duplicate: