@@ -11,42 +11,39 @@ async def main() -> None:
1111 cb_lock = asyncio .Event ()
1212
1313 async def callback (message : Message ) -> None :
14- print (f"[FROM_CALLBACK] { message .payload } " ) # noqa: T201
14+ print (f"[FROM_CALLBACK] { message .payload !r } " ) # noqa: T201
1515 cb_lock .set ()
1616
17- # When subscribing you can set callback.
18- # In that case CallbackSubscription is returned.
19- # This type of subscription cannot be iterated.
20- cb_sub = await nats .subscribe ("cb-subj" , callback = callback )
21-
22- # When callback is not set, you get a subscription
23- # that should be used along with `async for`
24- # loop, or alternatively you can call
25- # `await iter_sub.next()` to get a single message.
26- iter_sub = await nats .subscribe ("iter-subj" )
27-
28- # Subscriptions with queue argument create
29- # subscription with a queue group to distribute
30- # messages along all subscribers.
31- queue_sub = await nats .subscribe ("queue-subj" , queue = "example-queue" )
32-
33- await nats .publish ("cb-subj" , "message for callback" )
34- await nats .publish ("iter-subj" , "message for iterator" )
35- await nats .publish ("queue-subj" , "message for queue sub" )
36-
37- # We can unsubscribe after a particular amount of messages.
38- await iter_sub .unsubscribe (limit = 1 )
39- await cb_sub .unsubscribe (limit = 1 )
40- await queue_sub .unsubscribe (limit = 1 )
41-
42- async for message in iter_sub :
43- print (f"[FROM_ITERATOR] { message .payload } " ) # noqa: T201
44-
45- async for message in queue_sub :
46- print (f"[FROM_QUEUED] { message .payload } " ) # noqa: T201
47-
48- # Making sure that the message in callback is received.
49- await cb_lock .wait ()
17+ async with (
18+ # When subscribing you can set callback.
19+ # In that case CallbackSubscription is returned.
20+ # This type of subscription cannot be iterated.
21+ nats .subscribe ("cb-subj" , callback = callback ) as cb_sub ,
22+ # When callback is not set, you get a subscription
23+ # that should be used along with `async for`
24+ nats .subscribe ("iter-subj" ) as iter_sub ,
25+ # Subscriptions with queue argument create
26+ # subscription with a queue group to distribute
27+ # messages along all subscribers.
28+ nats .subscribe ("queue-subj" , queue = "example-queue" ) as queue_sub ,
29+ ):
30+ await nats .publish ("cb-subj" , "message for callback" )
31+ await nats .publish ("iter-subj" , "message for iterator" )
32+ await nats .publish ("queue-subj" , "message for queue sub" )
33+
34+ # We can unsubscribe after a particular amount of messages.
35+ await iter_sub .unsubscribe (limit = 1 )
36+ await cb_sub .unsubscribe (limit = 1 )
37+ await queue_sub .unsubscribe (limit = 1 )
38+
39+ async for message in iter_sub :
40+ print (f"[FROM_ITERATOR] { message .payload !r} " ) # noqa: T201
41+
42+ async for message in queue_sub :
43+ print (f"[FROM_QUEUED] { message .payload !r} " ) # noqa: T201
44+
45+ # Making sure that the message in callback is received.
46+ await cb_lock .wait ()
5047
5148 # Don't forget to call shutdown.
5249 await nats .shutdown ()
0 commit comments