Keep Track of Active Notifiers. Make Notifier usable as ContextManager. by zariiii9003 · Pull Request #1890 · hardbyte/python-can · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 160 additions & 19 deletions can/notifier.py
44 changes: 21 additions & 23 deletions examples/asyncio_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"""

import asyncio
from typing import List
from typing import TYPE_CHECKING

import can
from can.notifier import MessageRecipient

if TYPE_CHECKING:
from can.notifier import MessageRecipient


def print_message(msg: can.Message) -> None:
Expand All @@ -25,32 +27,28 @@ async def main() -> None:
reader = can.AsyncBufferedReader()
logger = can.Logger("logfile.asc")

listeners: List[MessageRecipient] = [
listeners: list[MessageRecipient] = [
print_message, # Callback function
reader, # AsyncBufferedReader() listener
logger, # Regular Listener object
]
# Create Notifier with an explicit loop to use for scheduling of callbacks
loop = asyncio.get_running_loop()
notifier = can.Notifier(bus, listeners, loop=loop)
# Start sending first message
bus.send(can.Message(arbitration_id=0))

print("Bouncing 10 messages...")
for _ in range(10):
# Wait for next message from AsyncBufferedReader
msg = await reader.get_message()
# Delay response
await asyncio.sleep(0.5)
msg.arbitration_id += 1
bus.send(msg)

# Wait for last message to arrive
await reader.get_message()
print("Done!")

# Clean-up
notifier.stop()
with can.Notifier(bus, listeners, loop=asyncio.get_running_loop()):
# Start sending first message
bus.send(can.Message(arbitration_id=0))

print("Bouncing 10 messages...")
for _ in range(10):
# Wait for next message from AsyncBufferedReader
msg = await reader.get_message()
# Delay response
await asyncio.sleep(0.5)
msg.arbitration_id += 1
bus.send(msg)

# Wait for last message to arrive
await reader.get_message()
print("Done!")


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions examples/cyclic_checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ def compute_xbr_checksum(message: can.Message, counter: int) -> int:

if __name__ == "__main__":
with can.Bus(channel=0, interface="virtual", receive_own_messages=True) as _bus:
notifier = can.Notifier(bus=_bus, listeners=[print])
cyclic_checksum_send(_bus)
notifier.stop()
with can.Notifier(bus=_bus, listeners=[print]):
cyclic_checksum_send(_bus)
15 changes: 7 additions & 8 deletions examples/print_notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
def main():
with can.Bus(interface="virtual", receive_own_messages=True) as bus:
print_listener = can.Printer()
notifier = can.Notifier(bus, [print_listener])

bus.send(can.Message(arbitration_id=1, is_extended_id=True))
bus.send(can.Message(arbitration_id=2, is_extended_id=True))
bus.send(can.Message(arbitration_id=1, is_extended_id=False))

time.sleep(1.0)
notifier.stop()
with can.Notifier(bus, listeners=[print_listener]):
# using Notifier as a context manager automatically calls `Notifier.stop()`
# at the end of the `with` block
bus.send(can.Message(arbitration_id=1, is_extended_id=True))
bus.send(can.Message(arbitration_id=2, is_extended_id=True))
bus.send(can.Message(arbitration_id=1, is_extended_id=False))
time.sleep(1.0)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion examples/send_multiple.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
This demo creates multiple processes of producers to spam a socketcan bus.
"""

from time import sleep
from concurrent.futures import ProcessPoolExecutor
from time import sleep

import can

Expand Down
2 changes: 1 addition & 1 deletion examples/serial_com.py
Loading