Yuan Fu
Yuan Fu

Reputation: 361

Watchdog observer method

I am new to python and watchdog, and was confused by the quickstart example. In the example, there is a block of code like this:

self.observer.start()
try:
    while True:
        time.sleep(5)
except:
    self.observer.stop()
    print('Error')

self.observer.join()

I couldn't find any documentations about the start, stop and join method. Also, although knowing that the while loop inside try except probably makes the observer run for every 5 seconds, I don't understand how does it work?

Could anyone explain me what do the three methods do and how does the loop work?

Upvotes: 5

Views: 5549

Answers (2)

Yuan Fu
Yuan Fu

Reputation: 361

Update: I corrected my answer based on @Amit Gupta's post.


After some research I found that all three methods belong to threading.Thread object.

self.observer.start() creates a new thread,

While True: time.sleep(1) keeps main thread running

When program stop, self.observer.stop() does some work before the thread terminate. (@Amit Gupta)

self.observer.join() is needed to proper end a thread for "it blocks the thread in which you're making the call, until (self.observer) is finished." as said by Erik Allik in Use of threading.Thread.join()

Upvotes: 5

Amit G
Amit G

Reputation: 196

threading.Thread does not provide any implementation for stop(). Read here. Stop() here is specific to Observer and in this example code it will stop the thread on receiving an exception. Rest of the stuff that you have written is correct.

Upvotes: 2

Related Questions