Reputation: 361
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
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