Skip to content

Commit 697fb01

Browse files
authored
Support for stopping NotificationReceiver.notifications() (#2090)
Details: * Added a `stop_event` parameter to the `NotificationReceiver.notifications` method that allows stopping the iterations over the HMC notifications if the event gets set. Signed-off-by: Andreas Maier <maiera@de.ibm.com>
1 parent 16ff14d commit 697fb01

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

changes/noissue.23.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added a `stop_event` parameter to the :meth:`zhmcclient.NotificationReceiver.notifications`
2+
method that allows stopping the iterations over the HMC notifications if the event
3+
gets set.

zhmcclient/_notification.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ def get_subscription(self, topic_name):
582582
return self._id_value(sub_id)
583583

584584
@logged_api_call
585-
def notifications(self):
585+
def notifications(self, stop_event=None):
586586
"""
587587
Generator method that yields all HMC notifications (= JMS messages)
588588
received by this notification receiver.
@@ -597,9 +597,19 @@ def notifications(self):
597597
thread; any errors do not cause the method to return but always cause
598598
an exception to be raised.
599599
600+
If a stop event is provided, the method performs checking for a stop.
601+
This is done by testing whether the stop event is set. If so, this
602+
method returns and the iteration of the caller on this method will end.
603+
600604
For an example how to use this method, see
601605
:ref:`Notifications` or the example scripts.
602606
607+
Parameters:
608+
609+
stop_event (threading.Event): Stop event that is checked. This can
610+
be used to end the iteration over the HMC notifications.
611+
If None, no stop checking is performed.
612+
603613
Yields:
604614
605615
: A tuple (headers, message) representing one HMC notification, with:
@@ -638,10 +648,6 @@ def notifications(self):
638648
formats" in chapter 4. "Asynchronous notification" in the
639649
:term:`HMC API` book.
640650
641-
Returns:
642-
643-
None
644-
645651
Raises:
646652
647653
:exc:`~zhmcclient.NotificationJMSError`: Received JMS error from
@@ -678,6 +684,10 @@ def notifications(self):
678684
try:
679685
item = self._handover_queue.get(timeout=ho_get_timeout)
680686
except queue.Empty:
687+
688+
if stop_event and stop_event.is_set():
689+
break
690+
681691
# This check detects a disconnect only when heartbeating is
682692
# enabled in the stomp retry/timeout configuration.
683693
if not self._conn.is_connected():
@@ -686,6 +696,12 @@ def notifications(self):
686696
continue
687697
break
688698

699+
if stop_event and stop_event.is_set():
700+
# This will cause the generator to return to Python. Python
701+
# will raise StopIteration, and the user loop on this method
702+
# will simply end.
703+
break
704+
689705
# Now we have an item from the listener
690706
if item.msgtype == 'message':
691707
if item.message is None:

0 commit comments

Comments
 (0)