Python basis of subprocess call exe

There are several ways to call exe, tell us here subprocess.

p = subprocess.Popen(“./XXX.exe param1 param2”, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

Return value p.returncode, exe message is printed out output = p.communicate () [0]

So some special consumption runtime resources, easy to get stuck, so set a timeout period, if you can analyze within the specified time, return analysis, if time-out, and killed exe, return to the default result.

def Func():
    p = subprocess.Popen("./XXX.exe", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    try:
        p.wait(timeout=SECONDS_TIMEOUT)
    except Exception as e:
        print("===== process timeout ======")
        p.kill()
        return None
    output= p.communicate()[0]
   err = p.communicate()[1]
   print(output)
   print(p.returncode)
 

 

 

Guess you like

Origin www.cnblogs.com/smart-zihan/p/11939748.html