Page 1 of 1

revpimodio2 with Python Multiprocessing

Posted: 06 Jun 2019, 13:49
by akash.sharma
I am running into some behavior I don't understand when I try to access the RevPi DIO module from parallel processes (using Python multiprocessing) using revpimodio2.

I have Process 1 and Process 2 which access DIFFERENT DIO outputs on the RevPi DIO. Let's say Process 1 sets outputs 1 and 2, and Process 2 sets outputs 3 and 4. Process 1 starts slightly before Process 2. I tried having both processes create their own connection objects using conn = revpimodio2.RevPiModIO(autorefresh=True) and I saw the following behavior (please note I have not experimented with setting autorefresh to False):

Process 1 behaved as expected, meaning when I set outputs 1 and 2 to Hi or Lo, the response was correct (tested using piTest and external multimeter).
However Process 2 did NOT behave as expected. When I set output 3 Hi I saw it fluctuated between Hi and Lo (tested using piTest and external multimeter). This led me to believe that somehow the "conn" object in Process 1 is somehow competing with the "conn" object in Process 2 BUT NOT THE OTHER WAY ROUND. Maybe Process 1 is higher priority because it started a little before?

So then I tried something different. I placed the conn = revpimodio2.RevPiModIO(autorefresh=True) OUTSIDE both Processes and in the function that LAUNCHES both Processes. Then I passed in this ONE "conn" object as a parameter into BOTH processes. Again, Process 1 starts slightly before Process 2. This time I saw the following behavior:

Process 1 again behaved as expected, meaning when I set outputs 1 and 2 to Hi or Lo, the response was correct (tested using piTest and external multimeter).
However Process 2 again did NOT behave as expected. This time when I set output 3 Hi it never went Hi (tested using piTest and external multimeter). This led me to believe that there is some dependency of this "conn" object that is locked to be used by Process 1 only?

Perhaps revpimodio2 is not built for multiprocessing? If so please let me know: I would need to do some rearchitecting, but I'm trying to avoid doing so if it is not necessary. If rearchitecting is necessary perhaps the solution is to create a single process that creates and accesses a "conn" object and let other processes that need help from RevPi DIO send requests to this new process? Maybe using a queue?

Thanks in advance!

Re: revpimodio2 with Python Multiprocessing

Posted: 02 Sep 2019, 21:13
by RevPiModIO
Hi akash.sharma!

RevPiModIO was not build to operate with two processes on the same process image. This was because of performance and safety! Your ONE control program should know the states of all set outputs and force them to the process image! Otherwise you have to check all outputs in your program an react to changes. This is not a save operating state for a PLC.

BUT!

In some cases it is important to run with multiple instances of RevPiModIO on the one RevPi. So now, with version 2.4.0 (apt-get dist-upgrade) you can set "direct_output=True" when you instantiate the RevPiModIO object. Than you can operate with more than one process and have all the functionality of RevPiModIO - The performance could drop a little if there are maaaaany outputs to set in one cycle. Give it a try!

Code: Select all

# For example
rpi = revpimodio2.RevPiModIO(autorefresh=True, direct_output=True)
Regards, Sven