0

I'm making a python interface to run a program in C and I need to know if this C program ended successfully or not, however the only way I know to do that locks the interface while the C program is not finished. Does anyone know how I can do this without blocking the interface?

Below is the code snippet:

def runProg(self):
    """This funcion will run the simulation"""

    if self.openned.get() ==  1:
        self.pid = StringVar()
        a = open(self.nameFile.get(),"w")
        self.writeFile()

        self.process = subprocess.Popen([self.cmdSys.get()+self.dV.get()+
                                         self.extension.get(),self.nameFile.get()])
        self.pid = self.process.pid

        #isso trava a interface para processos muito grandes até que o mesmo tenha    terminado
        if self.process.wait() == 0:
            tkMessageBox.showinfo("","Your simulation was completed sucessfully.")
Aya
  • 39,884
  • 6
  • 55
  • 55

3 Answers3

1

The self.process.wait() call will block until the subprocess has ended, but, as you have discovered, if you call it in the main GUI thread, it will prevent your GUI from processing any more events until that happens.

Instead, you can just check to see if it has ended with self.process.poll(), which will return None immediately if the process is still running.

However, you'll have to set up some sort of timed event to monitor the subprocess if you want something to happen when it's done.

Another option would be to start the subprocess in a background thread, and use the blocking wait() method instead. See this question for details.

Community
  • 1
  • 1
Aya
  • 39,884
  • 6
  • 55
  • 55
0

From subprocess module documentation:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

Run the command described by args. Wait for command to complete, then return the returncode attribute.

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

Run command with arguments. Wait for command to complete. If the return code was zero then return, otherwise raise CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute.

So I would use either subprocess.call() or subprocess.check_call() function intead of subprocess.Popen().

running.t
  • 5,329
  • 3
  • 32
  • 50
0

The subprocess Popen class has a returncode attribute, which, according to pydoc:

A None value indicates that the process hasn't terminated yet. A negative value -N indicates that the child was terminated by signal N (UNIX only).

hd1
  • 33,938
  • 5
  • 80
  • 91