How to replace p.wait() to maintain the functionality of the program

40 views Asked by At

I have three files in my program.

  1. commands.py
  2. argparser.py
  3. main.py

The commands.py runs argparse.py, and argparse.py runs the main.py program. commands.py:

def prog1():
    while True:
        p = Popen(['gnome-terminal', '--disable-factory', '--wait', '-e', 'python3 ./argparser.py --command '+command], preexec_fn=os.setpgrp)
        while True:
            with open("shared.txt", "r") as f: 
                number = int(f.read())
            if number > 5:
                os.killpg(p.pid, signal.SIGINT)
                break # want to restart that popen
            else:
                p.communicate()
                p.wait()
                print("Finished!")
                return # finish whole loops inside prog1()

I'm stuck at the communication stage. The main.py file writes a numerical value to the shared.txt file. I would like the program to end its operation and run again if the numerical value in the file is greater than 5. I would also like, if the value of this file is less than 5 wait program for closing.

Unfortunately, at the moment, if the value in the file is less than 5, it goes to p.wait() and blocks the entire loop, instead of still checking whether the file has finished running or the value has exceeded 5

Please help, Regards

EDIT AFTER COMMENTS

def prog1():
    while True:
        p = Popen(['gnome-terminal', '--disable-factory', '--wait', '-e', 'python3 ./argparser.py --command '+command], preexec_fn=os.setpgrp)
        while True:
            with open("shared.txt", "r") as f: 
                number = int(f.read())
            if number > 5:
                os.killpg(p.pid, signal.SIGINT)
                break # want to restart that popen
            else:
                value = p.stdout.readline()
                if value == "FINISHED":
                    return
                elif value == "WORKING":
                    continue

main.py

while True:
    ....
    .....
    ... some code...
    time.sleep(2)
    sys.stdout.write("Working")
    ....
    ...some code...
    time.sleep(2)
    sys.stdout.write("FINISHED")
    break

But... Code still stuck on p.stdout code and wait on break in main.py to recive data WorkingFinished. My code should READ live generated stdout.write (not wait on "break") and if it "working" - continue loop, when its "FINISHED" break whole loops, not stuck and wait as before to finish main.py

Any help?

0

There are 0 answers