55"""
66
77import asyncio
8- from typing import List
8+ from typing import TYPE_CHECKING , List
99
1010import can
11- from can .notifier import MessageRecipient
11+
12+ if TYPE_CHECKING :
13+ from can .notifier import MessageRecipient
1214
1315
1416def print_message (msg : can .Message ) -> None :
@@ -31,26 +33,22 @@ async def main() -> None:
3133 logger , # Regular Listener object
3234 ]
3335 # Create Notifier with an explicit loop to use for scheduling of callbacks
34- loop = asyncio .get_running_loop ()
35- notifier = can .Notifier (bus , listeners , loop = loop )
36- # Start sending first message
37- bus .send (can .Message (arbitration_id = 0 ))
38-
39- print ("Bouncing 10 messages..." )
40- for _ in range (10 ):
41- # Wait for next message from AsyncBufferedReader
42- msg = await reader .get_message ()
43- # Delay response
44- await asyncio .sleep (0.5 )
45- msg .arbitration_id += 1
46- bus .send (msg )
47-
48- # Wait for last message to arrive
49- await reader .get_message ()
50- print ("Done!" )
51-
52- # Clean-up
53- notifier .stop ()
36+ with can .Notifier (bus , listeners , loop = asyncio .get_running_loop ()):
37+ # Start sending first message
38+ bus .send (can .Message (arbitration_id = 0 ))
39+
40+ print ("Bouncing 10 messages..." )
41+ for _ in range (10 ):
42+ # Wait for next message from AsyncBufferedReader
43+ msg = await reader .get_message ()
44+ # Delay response
45+ await asyncio .sleep (0.5 )
46+ msg .arbitration_id += 1
47+ bus .send (msg )
48+
49+ # Wait for last message to arrive
50+ await reader .get_message ()
51+ print ("Done!" )
5452
5553
5654if __name__ == "__main__" :
0 commit comments