pythonconcurrencypython-asynciocan-buspython-can

Python Concurrency - CAN Bus


I'm in the beginning stages of developing a scale-able CAN based hardware test system. The test system's 2 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 asyncIO, 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 asyncIO built in, so that leads me to believe asyncIO should be fine. I'm worried that I may have to establish some kind of buffering with asyncIO if the test case coroutine doesn't process fast enough. The bus speed 500kb. There are 2 modules on the CAN bus. Highest message frequency is 20mS I assume that's enough time for asyncIO to run 2 coroutines. I'm also concerned with scalability.

Are my assumptions correct? Should it be just fine to use asyncIO?

Any and all input is greatly appreciated.


Solution

  • Seeing that your project involves Networking and Interprocess Communication, asyncIO 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 asyncIO 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 asyncIO there are some very useful methods of the event loop that interact well with threads. See link: https://docs.python.org/3/library/asyncio-dev.html#asyncio-multithreading

    Best of luck!