Multiple Processes


I just get started to use Arduino Yun and I researched a few ways to transfer data from Linux side to microcontroller.

Is it possible to have multiple Process objects running at same time and multitasking on Linux?


Yes. I routinely run several independent Process objects in my sketches.

The limitation is that one Process object can only run one Linux process. That means that you cannot use a single Process object to start a Linux process asynchronously, and then use that same Process object to start another Linux process. As soon as you try to do that, the first Linux process is terminated.

However, if you create multiple Process object instances, then each one can be running its own Linux process, and communicating with it independently. If you do want them to run at the same time, then you must run them asynchronously, and not use the regular run command.

One issue I’ve run into when talking to a Python script on the Linux side is that by default Python buffers its output. If the Python script is sending out little bits of data over time, the Process object in the sketch won’t see anything until the buffer fills up, at which point it gets a lot of data at once. The answer is to run the Python script in unbuffered mode by adding the -u option to the Python command line that runs the script. Note that this is not an issue if the script is short, and the output is sent as soon as the Python script terminates, even if it has not yet filled up a buffer.

Thank you ShapeShifter!

Just one more question. How is the speed of the execution of Process? I'm currently doing a project that requires writing data to a file in a millisecond period. I use FileIO lib directly writes data from Arduino to SD but it seems it's not fast enough and missing some data. I'm considering use Process instead.

I've not done any objective timing tests, but the Process communications speed has always been good enough for me, although granted, I've not used it for any high speed activities.

While the Linux side has reasonable performance, the '32U4 processor is relatively slow, and the communications between the two processors is over a serial link bottleneck. There is a limit to how much data you can force through that serial connection, but I'm sure it's faster than using FileIO.

When I design a Yun project, I try to do as much processing as possible on the Linux side, and do all data/network/file IO on the Linux side. I try to make the sketch as simple as possible and act as a semi-intelligent I/O processor. The sketch reads sensors and external inputs, and send the data down to the Linux side for processing. The Linux code does the major processing, makes any decisions, and does any data storage/access and networking. The Linux code then sends the results of that processing back up to the sketch which outputs that data to any external devices or outputs.

My goal in the design is to do as much processing on the Linux side with as little data as possible transferred between the two processors. That means that if the sketch can do some pre-processing of the data to minimize the amount of data transmitted, I will violate the "as much processing as possible on the Linux side" goal and do that pre-processing in the sketch. For example, when reading analog inputs, if I want a short-term average of the data, I will do that averaging in the sketch and send the average value down rather than every individual sample. One system I have reads the analog input frequently, and sends the average value to the Linux side once per second. While the averaging is done in the sketch, the data logging and decisions on how to react to the data are still done on the Linux side.

When deciding how to allocate the functionality of your project, keep in mind that the Linux side is fast, the sketch is slow, and the communications channel between them is even slower.