revpimodio2 with Python Multiprocessing
Posted: 06 Jun 2019, 13:49
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!
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!