diff --git a/python2/pyinotify.py b/python2/pyinotify.py index d2f0816..899645c 100755 --- a/python2/pyinotify.py +++ b/python2/pyinotify.py @@ -69,6 +69,7 @@ def __init__(self, version): import re import asyncore import subprocess +import weakref try: from functools import reduce @@ -675,7 +676,7 @@ def __init__(self, wm, notifier): @type notifier: Notifier instance """ self._watch_manager = wm # watch manager - self._notifier = notifier # notifier + self._notifier = weakref.ref(notifier) # notifier self._mv_cookie = {} # {cookie(int): (src_path(str), date), ...} self._mv = {} # {src_path(str): (dst_path(str), date), ...} @@ -733,7 +734,7 @@ def process_IN_CREATE(self, raw_event): # This path should not be taken. continue rawevent = _RawEvent(created_dir_wd, flags, 0, name) - self._notifier.append_event(rawevent) + self._notifier().append_event(rawevent) except OSError, err: msg = "process_IN_CREATE, invalid directory %s: %s" log.debug(msg % (created_dir, str(err))) @@ -1159,6 +1160,9 @@ def __init__(self, watch_manager, default_proc_fun=None, read_freq=0, # set of str(raw_event), only used when coalesce option is True self._eventset = set() + def __del__(self): + self.stop() + def append_event(self, event): """ Append a raw event to the event queue. diff --git a/python3/pyinotify.py b/python3/pyinotify.py index bc24313..29f5456 100755 --- a/python3/pyinotify.py +++ b/python3/pyinotify.py @@ -72,6 +72,7 @@ def __init__(self, version): import glob import locale import subprocess +import weakref try: from functools import reduce @@ -658,7 +659,7 @@ def __init__(self, wm, notifier): @type notifier: Notifier instance """ self._watch_manager = wm # watch manager - self._notifier = notifier # notifier + self._notifier = weakref.ref(notifier) # notifier self._mv_cookie = {} # {cookie(int): (src_path(str), date), ...} self._mv = {} # {src_path(str): (dst_path(str), date), ...} @@ -716,7 +717,7 @@ def process_IN_CREATE(self, raw_event): # This path should not be taken. continue rawevent = _RawEvent(created_dir_wd, flags, 0, name) - self._notifier.append_event(rawevent) + self._notifier().append_event(rawevent) except OSError as err: msg = "process_IN_CREATE, invalid directory: %s" log.debug(msg % str(err)) @@ -1142,6 +1143,9 @@ def __init__(self, watch_manager, default_proc_fun=None, read_freq=0, # set of str(raw_event), only used when coalesce option is True self._eventset = set() + def __del__(self): + self.stop() + def append_event(self, event): """ Append a raw event to the event queue.