question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] "Removable" event handlers.

See original GitHub issue

I’m happy to open a PR for this immediately if the feature seems useful.

Ignite’s event interface provides both add and remove operations however the “remove” operation requires a reference to the original event handler. This makes temporary or one-off event handlers somewhat tricky to implement.

Pytorch’s existing hook interface provides a solution to this issue by returning a “RemovableHandle” handle from register_hook, holding a weakref to the target hook to support later removal.

Ignite’s add_event_handler should implement a similar interface, returning a “RemovableEventHandle” rather than None. For example, in an attrs-style syntax:

@attr.s(auto_attribs=True)
class RemovableEventHandle:
    """Removable weak reference to an added event handler."""


    event_name: str
    handler: weakref.ref = attr.ib(converter=_as_weakref)
    engine: weakref.ref = attr.ib(converter=_as_weakref)

    def remove(self):
        """Remove handler from engine."""
        handler = self.handler()
        engine: Engine = self.engine()

        if handler and engine and engine.has_event_handler(handler):
            engine.remove_event_handler(handler, self.event_name)

    def __enter__(self):  # noqa: D
        return self

    def __exit__(self, type, value, tb):  # noqa: D
        self.remove()

Would support features like:

engine = Engine(process_function)

def print_epoch(engine):
    print("Epoch: {}".format(engine.state.epoch))

# Train with a temporary event handler.
with engine.add_event_handler(Events.EPOCH_COMPLETED, print_epoch):
    engine.run(dataloader, 10)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
asfordcommented, Aug 23, 2019

Probably, we could integrate it without attrib as a new dependency… What do yout think ?

Absolutely, I’ll open a PR with a vanilla implementation shortly.

1reaction
asfordcommented, Aug 23, 2019

Yes, there are multiple ways of implementing the equivalent behavior for single-shot handlers. I’ve just found this method to be low-overhead and relatively easy to keep tidy.

As you point out, I’m also using this feature to “reuse” an evaluator engine across multiple datasets with different logging configurations.

I think the context manger support offered by this feature is the key to increased usability.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Clean event handler invocation with C# 6
When an event handler unsubscribes, the value of the field storing the handlers changes. That can happen between checking and using the field...
Read more >
Event delegation - The Modern JavaScript Tutorial
In the handler we get event.target to see where the event actually happened and handle it. Let's see an example – the Ba-Gua...
Read more >
jQuery Event Extensions
jQuery offers several ways to extend its event system to provide custom functionality when events are attached to elements.
Read more >
Workaround for generic event handler in Windows Forms
You may consider filing a feature request on Microsoft Connect, ... i am adding this map *only* so that the removal of an...
Read more >
1. Document Object Model Events
This method allows the removal of event listeners from the event target. If an EventListener is removed from an EventTarget while it is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found