Basic usage?

866 views
Skip to first unread message

GreenAsJade

unread,
Jul 10, 2011, 7:01:00 PM7/10/11
to watchdog-python
Hi,

This module is very cool: perfect for what I need.

However, I'm finding it hard to work out how to use it.

For example, the quickstart shows using Observer methods start(),
stop() and join(),
but I can't find these documented anywhere.

What does "join()" do?

Is it OK to stop() an observer and start() it again: will that do the
"logical" thing?

Thanks!

Martin

GreenAsJade

unread,
Jul 10, 2011, 7:05:46 PM7/10/11
to watchdog-python


On Jul 11, 8:01 am, GreenAsJade <martin.jg.greg...@gmail.com> wrote:

> Is it OK to stop() an observer and start() it again: will that do the
> "logical" thing?

I found that the answer is "no". An observer can apparently only be
start()ed once.

So how do I pause the observer while I do things that I don't
want it to worry about, then restart it?

Thanks!

GreenAsJade

unread,
Jul 10, 2011, 7:07:40 PM7/10/11
to watchdog-python
Here's my test code, FWIW


from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from watchdog.events import LoggingEventHandler


class my_handler(FileSystemEventHandler):
def __init__(self):
FileSystemEventHandler.__init__(self)

def on_any_event(self, event):
print "yay - I saw the event"


if __name__ == "__main__":
event_handler = my_handler()
observer = Observer()
observer.schedule(event_handler, path=r"d:/H2HHelper/",
recursive=True)
observer.start()
try:
while True:
time.sleep(1)
print "tick"
except KeyboardInterrupt:
observer.stop()
print "do something, I won't see it"
time.sleep(10)
print "I hope I look again now..."
observer.start()
try:
while True:
time.sleep(1)
print "tick"
except KeyboardInterrupt:
observer.stop()
observer.join()

Zakhur

unread,
Jul 10, 2012, 8:33:08 PM7/10/12
to watchdo...@googlegroups.com

start() stop() , and join() aren't from this module, but rather from Threading and/or multiprocessing.

start() starts a Thread or Process, and stop() kills it.  .join() is what you use in the parent process to block until all Threads/Processes have finished.

So the answer is NO.  For that functionality you need something else  most likely, threading.RLock() or multiprocessing.RLock() would give you what you want.  Err, use threading because watchdog does.

I am considering a streamlining of watchdog for linux only with many fewer options, in an attempt to find stable code for a Webapp that needs to respond to file changes in one directory.  If I do it, it may be available or not, depending on my employer, but it will definitely use multiprocessing.

Zak

Martin Gregory

unread,
Jul 10, 2012, 9:17:27 PM7/10/12
to watchdo...@googlegroups.com
On 11/07/2012 10:03 AM, Zakhur wrote:


On Sunday, July 10, 2011 4:01:00 PM UTC-7, GreenAsJade wrote:
Hi,

This module is very cool: perfect for what I need.

However, I'm finding it hard to work out how to use it.

For example, the quickstart shows using Observer methods start(),
stop() and join(),
but I can't find these documented anywhere.




start() stop() , and join() aren't from this module, but rather from Threading and/or multiprocessing.


This is not obvious to me.  I have (basically):


from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

# blah blah
               
                self._observer = Observer()
                # blah blah
                self._observer.start()

It sure looks like this a start() method from the Observer module?

If it wasn't clear what I was asking: I think this is it :)

Thanks!

GaJ

Nik Kolev

unread,
May 17, 2013, 3:35:19 PM5/17/13
to watchdo...@googlegroups.com, mar...@gregories.net
an Observer is:
- which _Observer is defined to be one the concrete BaseObserver-s depending on the underlying os (https://github.com/gorakhargosh/watchdog/blob/master/src/watchdog/observers/api.py)
- which BaseObserver is an EventDispatcher (api.py again)
- which EventDispatcher is a DaemonThread (api.py yet again)

so yeah, threading.Thread...
Reply all
Reply to author
Forward
0 new messages