[Feature Request] "Removable" event handlers.
See original GitHub issueI’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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Absolutely, I’ll open a PR with a vanilla implementation shortly.
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.