+def popenTimeout2(cmd, pinput, returncode_ok=[0], ptimeout=21):
+ logging.debug("popenTimeout2: starting: " + cmd)
+ try:
+ sppoo = subprocess.Popen(cmd, stdin=subprocess.PIPE, shell=True)
+ if sys.hexversion >= 0x020600F0:
+ old_shandler = signal.signal(signal.SIGALRM,lambda sn,sf: sppoo.kill())
+ else:
+ old_shandler = signal.signal(signal.SIGALRM,lambda sn,sf: os.system("kill -9 %d" % sppoo.pid))
+ signal.alarm(ptimeout) #schedule alarm
+ sppoo.communicate(input=pinput)
+ sppoo.wait()
+ signal.alarm(0) #disable pending alarms
+ signal.signal(signal.SIGALRM, old_shandler)
+ logging.debug("popenTimeout2: subprocess %d finished, returncode: %d" % (sppoo.pid,sppoo.returncode))
+ if sppoo.returncode < 0:
+ logging.error("popenTimeout2: subprocess took too long (>%ds) and pid %d was killed" % (ptimeout,sppoo.pid))
+ return (sppoo.returncode in returncode_ok)
+ except Exception, e:
+ logging.error("popenTimeout2: "+str(e))
+ try:
+ signal.signal(signal.SIGALRM, old_shandler)
+ except:
+ pass
+ return False
+