diff --git a/examples/virtual_can_demo.py b/examples/virtual_can_demo.py index 8845fc8d2..b69fb28da 100644 --- a/examples/virtual_can_demo.py +++ b/examples/virtual_can_demo.py @@ -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)