-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plumpy.ProcessListener made persistent
solves #273 We implement the persistence of ProcessListener by deriving the class ProcessListener and EventHelper from persistence.Savable. The class EventHelper is moved to a new file because of a circular import with utils and persistence Fixing the test There was a circular reference issue in the test listener that was storing a reference to the process inside it, making its serialization impossible. To fix the tests an ugly hack was used: storing the reference to the process outside the class in a global dict using id as keys. Some more ugly hacks are needed to check correctly the equality of two processes. We must ignore the fact that the instances if the listener are different. We call del on dict items of the ProcessListener's global implemented in the test suite to clean the golbal variables addressed issues in #274
- Loading branch information
Showing
8 changed files
with
209 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# -*- coding: utf-8 -*- | ||
import logging | ||
from typing import TYPE_CHECKING, Any, Callable, Set, Type | ||
|
||
from . import persistence | ||
|
||
if TYPE_CHECKING: | ||
from .process_listener import ProcessListener # pylint: disable=cyclic-import | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
@persistence.auto_persist('_listeners', '_listener_type') | ||
class EventHelper(persistence.Savable): | ||
|
||
def __init__(self, listener_type: 'Type[ProcessListener]'): | ||
assert listener_type is not None, 'Must provide valid listener type' | ||
|
||
self._listener_type = listener_type | ||
self._listeners: 'Set[ProcessListener]' = set() | ||
|
||
def add_listener(self, listener: 'ProcessListener') -> None: | ||
assert isinstance(listener, self._listener_type), 'Listener is not of right type' | ||
self._listeners.add(listener) | ||
|
||
def remove_listener(self, listener: 'ProcessListener') -> None: | ||
self._listeners.discard(listener) | ||
|
||
def remove_all_listeners(self) -> None: | ||
self._listeners.clear() | ||
|
||
@property | ||
def listeners(self) -> 'Set[ProcessListener]': | ||
return self._listeners | ||
|
||
def fire_event(self, event_function: Callable[..., Any], *args: Any, **kwargs: Any) -> None: | ||
"""Call an event method on all listeners. | ||
:param event_function: the method of the ProcessListener | ||
:param args: arguments to pass to the method | ||
:param kwargs: keyword arguments to pass to the method | ||
""" | ||
if event_function is None: | ||
raise ValueError('Must provide valid event method') | ||
|
||
# Make a copy of the list for iteration just in case it changes in a callback | ||
for listener in list(self.listeners): | ||
try: | ||
getattr(listener, event_function.__name__)(*args, **kwargs) | ||
except Exception as exception: # pylint: disable=broad-except | ||
_LOGGER.error("Listener '%s' produced an exception:\n%s", listener, exception) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.