diff --git a/luigi/task.py b/luigi/task.py index f5a108dc23..4d3a312884 100644 --- a/luigi/task.py +++ b/luigi/task.py @@ -264,6 +264,13 @@ def wrapped(callback): return callback return wrapped + @classmethod + def remove_event_handler(cls, event, callback): + """ + Function to remove the event handler registered previously by the cls.event_handler decorator. + """ + cls._event_callbacks[cls][event].remove(callback) + def trigger_event(self, event, *args, **kwargs): """ Trigger that calls all of the specified events associated with this class. diff --git a/test/event_callbacks_test.py b/test/event_callbacks_test.py index 1d24752214..5382438628 100644 --- a/test/event_callbacks_test.py +++ b/test/event_callbacks_test.py @@ -162,6 +162,21 @@ def test_processing_time_handler_failure(self): t, result = self._run_processing_time_handler(True) self.assertEqual(result, []) + def test_remove_event_handler(self): + run_cnt = 0 + + @EmptyTask.event_handler(luigi.Event.START) + def handler(task): + nonlocal run_cnt + run_cnt += 1 + + task = EmptyTask() + build([task], local_scheduler=True) + assert run_cnt == 1 + EmptyTask.remove_event_handler(luigi.Event.START, handler) + build([task], local_scheduler=True) + assert run_cnt == 1 + # A # / \