diff --git a/python2/pyinotify.py b/python2/pyinotify.py index 2dae002..1ea7e4b 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 @@ -708,7 +709,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), ...} @@ -766,7 +767,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))) @@ -1191,6 +1192,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 df4034a..4814f6b 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 @@ -698,7 +699,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), ...} @@ -756,7 +757,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)) @@ -1181,6 +1182,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.