File: send_multiple.py

package info (click to toggle)
python-can 4.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,372 kB
  • sloc: python: 25,840; makefile: 38; sh: 20
file content (36 lines) | stat: -rwxr-xr-x 1,078 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3

"""
This demo creates multiple processes of producers to spam a socketcan bus.
"""

from time import sleep
from concurrent.futures import ProcessPoolExecutor

import can


def producer(thread_id: int, message_count: int = 16) -> None:
    """Spam the bus with messages including the data id.

    :param thread_id: the id of the thread/process
    :param message_count: the number of messages that shall be sent
    """

    # this uses the default configuration (for example from environment variables, or a
    # config file) see https://python-can.readthedocs.io/en/stable/configuration.html
    with can.Bus() as bus:
        for i in range(message_count):
            msg = can.Message(
                arbitration_id=0x0CF02200 + thread_id,
                data=[thread_id, i, 0, 1, 3, 1, 4, 1],
            )
            bus.send(msg)
        sleep(1.0)

    print(f"Producer #{thread_id} finished sending {message_count} messages")


if __name__ == "__main__":
    with ProcessPoolExecutor() as executor:
        executor.map(producer, range(5))