0

I want to first run a python file(parent) which automatically executes another python file(child) and send data(specifically a list) from the parent file to the child file.

I cannot import one file into another since the parent file has to execute a lot of blocking commands and I need the child file to publish continuously.

I initially tried Popen subprocess with which I was able to run both at the same time but I wasn't able to catch any output from the parent in the child class. Simply writing data to a file then reading it from the child class could work but I wasn't able to get it working reliably since it only read once; and closing the file to open it again to read seemed way too hacky and slow.

So how is this normally done? Here's a super-simplified version of the code:-

File 1(Parent file):-

list = [0,0,0]
class CmdDecide:
    def __init__(self, param1=50):
        //Some code
        //I tried subprocess.run(['python3',file2.py]) to run the 2 files simultaneously but I need to send the list variable to it
    
    def func_A(self, var_from_someplace_else):
        global list
        list = var_from_someplace_else
        sendList(list)
    
    def sendList(self):
        //Code to send this list to file 2 somehow
    
    //There are a lot more functions in this file

File 2(Child file):-

def main():
    //Send continuous output [0,0,0] till File 1 changes the array
    //It will continuously output the new array and continue to do so till the program is terminated

Note: Ik this exists Run one Python code in the background from another Python code but the pipe has not been implemented and I'm not sure if it can even handle lists.

Thugunb
  • 11
  • 1
  • 1
  • 3

1 Answers1

0

If the main concern of merging the two files into one is blocking processes then run the parent file methods as async (https://docs.python.org/3/library/asyncio.html). Invoking one file as a sub-process is not in my opinion the best way to go forwards as it decouples the logic but maintains the dependency. If you need to invoke the file as a sub-process then look into argpase and pass the list as a argument to

subprocess.run(['python3',file2.py])

m-eriksen
  • 61
  • 1
  • 4