pythonconcurrencypython-asynciocan-buspython-can

Python concurrency - CAN bus


I'm in the beginning stages of developing a scalable CAN-based hardware test system. The test system's two main functions are to log all traffic on the bus and execute test cases. The test cases will mostly be composed of receiving and transmitting CAN messages, while evaluating the contents of the message data payloads. There will also be a series of test cases that will have to integrate with a serial-controlled device.

Due to the importance of logging all CAN traffic, I have been considering a concurrency based program. But I am not sure which form of concurrency I should implement. I've read about asynchronous I/O, threading and multiprocessing. So I have a high level view of each of them.

I'm also using the python-can library which seems to have asynchronous I/O built in, so that leads me to believe asynchronous I/O should be fine. I'm worried that I may have to establish some kind of buffering with asynchronous I/O if the test case coroutine doesn't process fast enough. The bus speed is 500 kbit/s. There are two modules on the CAN bus. The highest message frequency is 20 ms (corresponding to 50 Hz), and I assume that's enough time for asynchronous I/O to run two coroutines. I'm also concerned with scalability.

Are my assumptions correct? Should it be just fine to use asynchronous I/O?


Solution

  • Seeing that your project involves networking and inter-process communication, asynchronous I/O should be most reliant and heavily incorporated method of concurrency. It is not likely that you will need threading or multiprocessing, unless you are doing a lot of communication between hosts. Even then, there is significant overhead that comes with spawning processes or threads, that may even slow down the execution time if not used carefully.

    My final recommendation is to stick with asynchronous I/O for now and once you have a prototype of your project, you can try incorporating threading/multiprocessing and see if you notice a speed-up. There are modules such as timeit that are perfect for this purpose. If you do chose to use threading with asynchronous I/O there are some very useful methods of the event loop that interact well with threads. See Concurrency and Multithreading.

    Best of luck!