Reputation: 7387
I am programming a tool which adds files to a processing chain. I want to monitor a specific known directory every 10s, compare, and send the results to my already existing functions.
I wrote a short class:
class Watchdog(Thread):
def __init__(self, path):
""" Constructor """
self.watch_folder = path
self.verbose = True
pass
def run(self):
"""
we check every 10s
"""
sleep_duration = 10
before = dict ([(f, None) for f in os.listdir (self.watch_folder)])
while True:
time.sleep(sleep_duration)
after = dict ([(f, None) for f in os.listdir (self.watch_folder)])
added_files = [f for f in after if not f in before]
removed_files = [f for f in before if not f in after]
if added_files and self.verbose:
print "Added: ", ", ".join (added_files)
if removed_files and self.verbose:
print "Removed: ", ", ".join (removed_files)
before = after
return added_files
I understand that due to the endless loop I cannot easily return the data. Especially because the rest of the program is imperative
if __name__ == '__main__':
functionA()
do_smth()
# added_files is returned from the Thread and ideally changing.
if added_files >= n:
for file in added_files:
# happycode goes here, but how do I know about the change each time?
In particular: can I simply (without using queue models or anything crazily complex) return from the thread? I would like to start the processing functions once the Thread can report a change (so this is my Observer).
I wonder if there is a simpler way to return from the endless loop so that the rest of the program can remain imperative.
Upvotes: 1
Views: 103
Reputation: 56
http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html
EDIT: I know 1 and 2, don't answer your question on infinite loop, but are alternatives. Also,
Hope this helps
Upvotes: 3