Skip to content

virtual_can_demo.py changes #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 30, 2018
Merged
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
32 changes: 16 additions & 16 deletions examples/virtual_can_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
# coding: utf-8

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

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

import can


def producer(id):
""":param id: Spam the bus with messages including the data id."""
def producer(id, message_count=16):
"""Spam the bus with messages including the data id.

bus = can.interface.Bus(bustype='socketcan', channel='vcan0')
for i in range(16):
msg = can.Message(arbitration_id=0x0cf02200+id, data=[id, i, 0, 1, 3, 1, 4, 1])
bus.send(msg)
:param int id: the id of the thread/process
"""

with can.Bus(bustype='socketcan', channel='vcan0') as bus:
for i in range(message_count):
msg = can.Message(arbitration_id=0x0cf02200+id, data=[id, i, 0, 1, 3, 1, 4, 1])
bus.send(msg)
sleep(1.0)

print("Producer #{} finished sending {} messages".format(id, message_count))

# TODO Issue #3: Need to keep running to ensure the writing threads stay alive. ?
time.sleep(2)

if __name__ == "__main__":
#logging.getLogger('').setLevel(logging.DEBUG)
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
with ProcessPoolExecutor(max_workers=4) as executor:
executor.map(producer, range(5))

time.sleep(2)