Subprocess command fails to return

2019-05-30 10:32发布

问题:

I have a thread which handles commands sent to a device. It opens a subprocess, sends the command to qmicli application (https://sigquit.wordpress.com/2012/08/20/an-introduction-to-libqmi/), gets a reply and the reply is dealt with.

This generally works fine for days/weeks of running. However I noticed that sometimes the thread would sometimes just stop doing anything when I make a subprocess.Popen call (the next lines of code do not run), the simplified code looks like this:

try:                
    self.qmi_process = subprocess.Popen(cmd,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.STDOUT)
    # Log value of self.qmi_process happens here
    if self.qmi_process:
       out = self.qmi_process.communicate()
    else: 
        return "ERROR: no qmi_process"
    self.qmi_process = None 
    ret = ''.join(str(e) for e in out if e)
except:
    return "ERROR: Caught unhandled exception" 

I have started logging the value of the subprocess.Popen call to see if the communicate() call was blocking or was it failing before this when the subprocess call is created. It turns out that for some reason the subprocess.Popen fails and self.qmi_process value is not logged, but my Exception code is not being called, any idea how that could happen?

subprocess.Popen does not return.

I have multiple threads calling popen, I've read this can cause deadlock in 2.7?