Skip to content

Commit 312784e

Browse files
committed
Updated/rewrote toolkit listener implementation.
1 parent 92932f6 commit 312784e

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

samples/notification2_asynchronous.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ async def main():
2929
print(f"Subscription created: {sub_name}")
3030

3131
# Create a listener for previously created subscription
32-
listener = AsyncListener(c8y, sub.name)
32+
listener = AsyncListener(
33+
c8y,
34+
subscription_name=sub.name,
35+
auto_ack=True,
36+
auto_unsubscribe=True,
37+
)
3338

3439
# Define callback function.
3540
# This function is invoked (asynchronously) for each received event.
3641
async def callback(msg:AsyncListener.Message):
3742
print(f"Received message, ID: {msg.id}, Source: {msg.source}, Action: {msg.action}, Body: {msg.json}")
38-
await msg.ack()
3943

4044
# Start listening
4145
listener.start(callback)
@@ -48,7 +52,7 @@ async def callback(msg:AsyncListener.Message):
4852
# The update event is now being processed
4953
await asyncio.sleep(5)
5054

51-
# close the listener and wait for it to end.
55+
# stop the listener and wait for it to end.
5256
listener.stop()
5357
await listener.wait()
5458

samples/notification2_synchronous.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,21 @@
3232
print(f"Subscription created: {sub_name}")
3333

3434
# Create a listener for previously created subscription
35-
listener = Listener(c8y, sub.name)
35+
listener = Listener(
36+
c8y,
37+
subscription_name=sub.name,
38+
auto_ack=False,
39+
auto_unsubscribe=False,
40+
)
3641

3742
# Define callback function.
3843
# This function is invoked (synchronously) for each received event.
3944
def callback(msg: Listener.Message):
4045
print(f"Received message, ID: {msg.id}, Source: {msg.source}, Action: {msg.action}, Body: {msg.json}")
4146
msg.ack()
4247

43-
# Wrap listen function into separate thread
44-
# and start listening
45-
listener_thread = threading.Thread(target=listener.listen, args=[callback])
46-
listener_thread.start()
48+
# Start listening, thread is handled automatically
49+
listener.start(callback)
4750

4851
# Some action: Update the managed object
4952
time.sleep(5)
@@ -53,9 +56,9 @@ def callback(msg: Listener.Message):
5356
# The update event is now being processed
5457
time.sleep(5)
5558

56-
# close the listener
59+
# stop the listener and wait for completion
5760
listener.stop()
58-
listener_thread.join()
61+
listener.wait()
5962

6063
# cleanup subscription and managed object
6164
sub.delete()

0 commit comments

Comments
 (0)