Skip to content

Commit 9c81965

Browse files
committed
use Notifier context manager in examples
1 parent 94f07d2 commit 9c81965

7 files changed

+37
-43
lines changed

examples/asyncio_demo.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
"""
66

77
import asyncio
8-
from typing import List
8+
from typing import TYPE_CHECKING, List
99

1010
import can
11-
from can.notifier import MessageRecipient
11+
12+
if TYPE_CHECKING:
13+
from can.notifier import MessageRecipient
1214

1315

1416
def 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

5654
if __name__ == "__main__":

examples/cyclic_checksum.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,5 @@ def compute_xbr_checksum(message: can.Message, counter: int) -> int:
5959

6060
if __name__ == "__main__":
6161
with can.Bus(channel=0, interface="virtual", receive_own_messages=True) as _bus:
62-
notifier = can.Notifier(bus=_bus, listeners=[print])
63-
cyclic_checksum_send(_bus)
64-
notifier.stop()
62+
with can.Notifier(bus=_bus, listeners=[print]):
63+
cyclic_checksum_send(_bus)

examples/print_notifier.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
def main():
99
with can.Bus(interface="virtual", receive_own_messages=True) as bus:
1010
print_listener = can.Printer()
11-
notifier = can.Notifier(bus, [print_listener])
12-
13-
bus.send(can.Message(arbitration_id=1, is_extended_id=True))
14-
bus.send(can.Message(arbitration_id=2, is_extended_id=True))
15-
bus.send(can.Message(arbitration_id=1, is_extended_id=False))
16-
17-
time.sleep(1.0)
18-
notifier.stop()
11+
with can.Notifier(bus, listeners=[print_listener]):
12+
# using Notifier as a context manager automatically calls `Notifier.stop()`
13+
# at the end of the `with` block
14+
bus.send(can.Message(arbitration_id=1, is_extended_id=True))
15+
bus.send(can.Message(arbitration_id=2, is_extended_id=True))
16+
bus.send(can.Message(arbitration_id=1, is_extended_id=False))
17+
time.sleep(1.0)
1918

2019

2120
if __name__ == "__main__":

examples/send_multiple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
This demo creates multiple processes of producers to spam a socketcan bus.
55
"""
66

7-
from time import sleep
87
from concurrent.futures import ProcessPoolExecutor
8+
from time import sleep
99

1010
import can
1111

examples/serial_com.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
com0com: http://com0com.sourceforge.net/
1919
"""
2020

21-
import time
2221
import threading
22+
import time
2323

2424
import can
2525

examples/vcan_filtered.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ def main():
1818
# print all incoming messages, which includes the ones sent,
1919
# since we set receive_own_messages to True
2020
# assign to some variable so it does not garbage collected
21-
notifier = can.Notifier(bus, [can.Printer()]) # pylint: disable=unused-variable
22-
23-
bus.send(can.Message(arbitration_id=1, is_extended_id=True))
24-
bus.send(can.Message(arbitration_id=2, is_extended_id=True))
25-
bus.send(can.Message(arbitration_id=1, is_extended_id=False))
26-
27-
time.sleep(1.0)
28-
notifier.stop()
21+
with can.Notifier(bus, [can.Printer()]): # pylint: disable=unused-variable
22+
bus.send(can.Message(arbitration_id=1, is_extended_id=True))
23+
bus.send(can.Message(arbitration_id=2, is_extended_id=True))
24+
bus.send(can.Message(arbitration_id=1, is_extended_id=False))
25+
time.sleep(1.0)
2926

3027

3128
if __name__ == "__main__":

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ ignore = [
167167
]
168168
"can/logger.py" = ["T20"] # flake8-print
169169
"can/player.py" = ["T20"] # flake8-print
170+
"examples/*" = ["T20"] # flake8-print
170171

171172
[tool.ruff.lint.isort]
172173
known-first-party = ["can"]

0 commit comments

Comments
 (0)