-
Notifications
You must be signed in to change notification settings - Fork 4
PTDM subscribes to relevant events only #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jcapona
wants to merge
9
commits into
master
Choose a base branch
from
ptdm-subscribes-to-relevant-events
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2c275bd
Subscribe only to relevant PTDM events
jcapona 0c0d458
Update tests
jcapona dfd18d0
Poll for events
jcapona d7eb29c
Update function default value
jcapona 53e38c8
invoke_callback_if_exists -> invoke_callback
jcapona bd81adb
Unskip test
jcapona 7e1b4f3
Test case where a callback with args is called
jcapona a1fe0a7
Merge remote-tracking branch 'origin/master' into ptdm-subscribes-to-…
jcapona 2873910
Increase test timeout
jcapona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -420,7 +420,7 @@ class PTDMSubscribeClient: | |
def __init__(self): | ||
self.__thread = Thread(target=self.__thread_method, daemon=True) | ||
|
||
self._callback_funcs = None | ||
self._callback_funcs = {} | ||
|
||
self._zmq_context = None | ||
self._zmq_socket = None | ||
|
@@ -435,7 +435,9 @@ def __exit__(self, exc_type, exc_value, exc_traceback): | |
def __connect_to_socket(self): | ||
self._zmq_context = zmq.Context() | ||
self._zmq_socket = self._zmq_context.socket(zmq.SUB) | ||
self._zmq_socket.setsockopt_string(zmq.SUBSCRIBE, "") | ||
|
||
for message_id in self._callback_funcs.keys(): | ||
self._zmq_socket.setsockopt(zmq.SUBSCRIBE, str(message_id).encode()) | ||
|
||
try: | ||
self._zmq_socket.connect(self.URI) | ||
|
@@ -463,35 +465,40 @@ def __thread_method(self): | |
poller.register(self._zmq_socket, zmq.POLLIN) | ||
while self.__continue: | ||
events = poller.poll(_TIMEOUT_MS) | ||
|
||
for _ in range(len(events)): | ||
message_string = self._zmq_socket.recv_string() | ||
message = Message.from_string(message_string) | ||
|
||
id = message.message_id() | ||
if id in self._callback_funcs: | ||
self.invoke_callback_func_if_exists( | ||
self._callback_funcs[id], message.parameters | ||
) | ||
callback = self._callback_funcs.get(message.message_id()) | ||
if callback: | ||
self.invoke_callback(callback, message.parameters) | ||
|
||
def invoke_callback_func_if_exists(self, func, params=list()): | ||
if not callable(func): | ||
return | ||
|
||
func_arg_no = len(signature(func).parameters) | ||
if func_arg_no > 1: | ||
logger.error( | ||
"Invalid callback function - it should receive at most one argument." | ||
) | ||
return "" | ||
def invoke_callback(self, callback, params=None): | ||
if params is None: | ||
params = list() | ||
|
||
if params == list() or func_arg_no == 0: | ||
func() | ||
func_arg_no = len(signature(callback).parameters) | ||
if len(params) == 0 or func_arg_no == 0: | ||
callback() | ||
else: | ||
func(params) | ||
callback(params) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. codecov seems to say this method isn't tested at all - maybe you could quickly add one where the callback is actually called? |
||
|
||
def initialise(self, callback_funcs): | ||
self._callback_funcs = callback_funcs | ||
for message_id, callback in callback_funcs.items(): | ||
if not callable(callback): | ||
logger.error( | ||
f"Invalid callback function for message {message_id} - not callable. Skipping..." | ||
) | ||
continue | ||
|
||
func_arg_no = len(signature(callback).parameters) | ||
if func_arg_no > 1: | ||
logger.error( | ||
f"Invalid callback function for message {message_id} - it should receive at most one argument. Skipping..." | ||
) | ||
continue | ||
|
||
self._callback_funcs.update({message_id: callback}) | ||
|
||
def start_listening(self): | ||
if not self.__connect_to_socket(): | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice