1919import asyncio
2020import contextlib
2121
22+ import cortex
2223from cortex import DictMessage , Node
2324from cortex .messages .base import MessageHeader
2425
@@ -28,19 +29,17 @@ class DictSubscriberNode(Node):
2829
2930 def __init__ (self ) -> None :
3031 super ().__init__ (name = "dict_subscriber" )
32+ # Create a subscriber - connection happens asynchronously in run()
3133 print ("Waiting for publisher on /robot/state..." )
32- sub = self .create_subscriber (
34+ self .create_subscriber (
3335 topic_name = "/robot/state" ,
3436 message_type = DictMessage ,
3537 callback = self ._on_state_received ,
3638 wait_for_topic = True ,
3739 topic_timeout = 30.0 ,
3840 )
3941
40- if not sub .is_connected :
41- raise RuntimeError ("Failed to connect to topic!" )
42-
43- print ("Connected! Receiving messages..." )
42+ print ("Subscriber created, will connect when run() is called..." )
4443 print ("Press Ctrl+C to stop" )
4544 print ()
4645
@@ -78,11 +77,7 @@ async def main() -> None:
7877 """Run the dictionary subscriber example."""
7978 print ("Starting dictionary subscriber..." )
8079
81- try :
82- node = DictSubscriberNode ()
83- except RuntimeError as e :
84- print (e )
85- return
80+ node = DictSubscriberNode ()
8681
8782 try :
8883 await node .run ()
@@ -95,4 +90,4 @@ async def main() -> None:
9590
9691if __name__ == "__main__" :
9792 with contextlib .suppress (KeyboardInterrupt ):
98- asyncio .run (main ())
93+ cortex .run (main ())
0 commit comments