2 years ago

#28896

test-img

izidor

Python 3.6 based programing implementation to exchange data between 2 modules running concurrently (each one forever)

I'm trying to implement data exchange between 3 python scripts. mp1.py responsible to start multiprocessing pipe with mp2.py in order to receive one byte of integer/per-second continuously. mp2.py holds a data list of integers, which gets updated randomly and acyncrously by 3rd python script script.py. The following buggy code is implemented in the Jetson Xavier NX development kit and can be reproduced on any standard hardware running Python 3.6.

Expected behavior: mp1.py receives one byte of integer each second. mp2.py synchronously sends the data to mp1.py from the list of integers. This list asynchronously feds by script.py based on random timing betwen 1sec. and 1 min.

Buggy behavior: mp1.py receives for ever, only one byte of the two - default bytes of the list.

mp1.py

   python

from multiprocessing import Process,Queue,Pipe
from mp2 import getData
import time

parent_conn,child_conn = Pipe()

def getDataByte():
    p = Process(target=getData, args=(child_conn,))
    p.start()
    a = parent_conn.recv()
    print("Recieved Data is:", a)


if __name__ == '__main__':  
    while True :
        time.sleep(1)
        getDataByte()

mp2.py

python

myl = [-1,-2]
mylConsumed = False

def getData(child_conn):
    print("Debug of getData")
    if not len(myl):
        print("myl is consumed")
        data = 0
        mylConsumed = True
    else:
            data = myl[0]
            myl.pop(0)
            print("myl list is: ", myl)
    child_conn.send(data)

def setData(thisBuffer):
    if mylConsumed == True:
        myl1 = thisBuffer
        myl.extend(myl1)
        mylConsumed = False
    print(myl)

script.py

   python

from mp2 import setData
from random import randint
import time

mybuf = [1,2,3,4,5,6,7,8,9,10]

def setBleData():
    setData(mybuf)

if __name__ == "__main__":
    while True:
        num = randint(1,5) 
        time.sleep(num)
        setBleData()

To reproduce the bug: 1st run python3 mp1.py command in the 1st terminal.
2nd run pyhthon3 script.py command in 2nd terminal.

The following is the 1st terminal output display till interrupting execution by CNTL+C :

python3 mp1.py
Debug of getData
myl list is:  [-2]
Recieved Data is: -1
Debug of getData
myl list is:  [-2]
Recieved Data is: -1
Debug of getData
myl list is:  [-2]
Recieved Data is: -1
Debug of getData
myl list is:  [-2]
Recieved Data is: -1
Debug of getData
myl list is:  [-2]
Recieved Data is: -1
Debug of getData
myl list is:  [-2]
Recieved Data is: -1
^CTraceback (most recent call last):
  File "mp1.py", line 16, in <module>
    time.sleep(1)
KeyboardInterrupt

This problem is understood and fixed. Many thanks to everyone involved!!!

The root cause as the described above is improper and not needed at all use of multiprocessing.

All variables and code gets copied to another process address space, hence each process gets stuff done on separate data and code.

After spending lots of hours digging into the problem, the solution is very simple - just import required functions from other python script files.

python

multiprocessing

pipe

nvidia-jetson

0 Answers

Your Answer

Accepted video resources