问题
I'm creating a program in Python using Watchdog that watches a set of files and takes actions based on changes. I put the exact example from their site in 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 odd. I have installed watchdog for both Python 2 and Python 3, in the same way (using pip2 install watchdog
and 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 I'm wondering is what could cause this behavior and how could I fix it.
This question is not a duplicate of:
- python watchdog runs more than once; the events are all the same
- Python watchdog duplicate events; the error only happens on Python 3, not on Python 2.
来源:https://stackoverflow.com/questions/32861420/watchdog-getting-events-thrice-in-python-3