Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding callbacks after instantiating FileSystemEventHandler doesn't fire the callback. #832

Closed
unique1o1 opened this issue Aug 22, 2021 · 1 comment

Comments

@unique1o1
Copy link
Contributor

unique1o1 commented Aug 22, 2021

OS: Pop!_OS 20.04 LTS
Python version: 3.9.5
Watchdog version: 2.1.4

Adding callbacks (on_deleted, on_created...) after instantiating FileSystemEventHandler class(and its subclasses) will not fire the respective callback when a event is trigger

For example:
This Works

class myEventHandler(RegexMatchingEventHandler):
    def on_created(self,event):
        print(f" created!")

    def on_deleted(self,event):
        print(f"deleted")

    def on_modified(self,event):
        print(f"modified")

    def on_moved(self,event):
        print(f"moved")
my_event_handler = myEventHandler(
    [".*"], None, False, False
)
path = "."
my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive=True)
my_observer.start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    my_observer.stop()
    my_observer.join()

This doesn't

def on_created(self,event):
    print(f" created!")
def on_deleted(self,event):
    print(f"deleted")
def on_modified(self,event):
    print(f"modified")
def on_moved(self,event):
    print(f"moved")

path = "."
my_event_handler = RegexMatchingEventHandler(
    [".*"], None, False, False
)
my_event_handler.on_created = on_created
my_event_handler.on_deleted = on_deleted
my_event_handler.on_modified = on_modified
my_event_handler.on_moved = on_moved

my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive=True)
my_observer.start()
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    my_observer.stop()
    my_observer.join()

changes from the PR #814 seems to be the reason for the issue.

EDIT: Sorry if this is not the preferred way of doing things. I just started using watchdog and many articles on the web used this approach and mine was not working, which got me scratching my head hard. I tried the above code on the older version i.e (2.1.3) and it worked.

@BoboTiG
Copy link
Collaborator

BoboTiG commented Aug 23, 2021

Thanks, it is a duplicate of #830. I am closing that one and focus on #830.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants