Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

python - Python看门狗多线程(Python watchdog multithreading)

I'm using watchdog to look at filenames with a specific pattern.

(我正在使用看门狗来查看具有特定模式的文件名。)

It is working great but I can't get multi-threading to work.

(它工作得很好,但是我无法使用多线程。)

Would be great if someone could point me in the right direction.

(如果有人能指出我正确的方向,那就太好了。)

I'm quite new to Python.

(我对Python很陌生。)

import time
from datetime import datetime

from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer


class Handler(PatternMatchingEventHandler):
    patterns = ["*_Log*.txt"]

    def on_modified(self, event):
        time_stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        print(time_stamp + " file modified: " + event.src_path)


class Watcher:
    def __init__(self, path):
        self.observer = Observer()
        self.path = path
        self.handler = Handler()

    def start(self):
        self.observer.schedule(self.handler, self.path, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(5)
        except Exception as error:
            self.observer.stop()
            print("Error: " + str(error))
        self.observer.join()


if __name__ == '__main__':
    w1 = Watcher('\\server1/c$/folderworklogs')
    w2 = Watcher('\\server2/c$/folderworklogs')
    w1.start()
    w2.start()
  ask by stan5079 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...