मैं साथ ही दो उपप्रोसेसेस चला रहा हूं, और आउटपुट को एक ही लॉग फ़ाइल में सहेज रहा हूं। मैंने लटका subprocesses को संभालने के लिए एक समय समाप्ति में भी बनाया है। जब आउटपुट बहुत बड़ा हो जाता है, तो टाइमआउट हमेशा ट्रिगर होता है, और किसी भी उपप्रजाति से stdout में से कोई भी लॉग फ़ाइल में सहेजा नहीं जाता है। उपरोक्त एलेक्स द्वारा उत्पन्न उत्तर इसे हल नहीं करता है।
# Currently open log file.
log = None
# If we send stdout to subprocess.PIPE, the tests with lots of output fill up the pipe and
# make the script hang. So, write the subprocess's stdout directly to the log file.
def run(cmd, logfile):
#print os.getcwd()
#print ("Running test: %s" % cmd)
global log
p = subprocess.Popen(cmd, shell=True, universal_newlines = True, stderr=subprocess.STDOUT, stdout=logfile)
log = logfile
return p
# To make a subprocess capable of timing out
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
log.flush()
raise Alarm
####
## This function runs a given command with the given flags, and records the
## results in a log file.
####
def runTest(cmd_path, flags, name):
log = open(name, 'w')
print >> log, "header"
log.flush()
cmd1_ret = run(cmd_path + "command1 " + flags, log)
log.flush()
cmd2_ret = run(cmd_path + "command2", log)
#log.flush()
sys.stdout.flush()
start_timer = time.time() # time how long this took to finish
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(5) #seconds
try:
cmd1_ret.communicate()
except Alarm:
print "myScript.py: Oops, taking too long!"
kill_string = ("kill -9 %d" % cmd1_ret.pid)
os.system(kill_string)
kill_string = ("kill -9 %d" % cmd2_ret.pid)
os.system(kill_string)
#sys.exit()
end_timer = time.time()
print >> log, "closing message"
log.close()
धन्यवाद। मैं कसम खा सकता था कि मैंने कोशिश की और एक त्रुटि मिली, लेकिन यह वही है जो मैं उम्मीद कर रहा था कि काम करेगा। –
यह मेरे लिए काम नहीं करता है। मैं एक साथ दो प्रक्रियाओं को चला रहा हूं और stdout और stderr दोनों को एक लॉग फ़ाइल में सेव कर रहा हूं। अगर आउटपुट बहुत बड़ा हो जाता है, तो उपप्रोसेस में से एक लटकता है; कौन नहीं जानता मैं एक टिप्पणी में स्वरूपण का उपयोग नहीं कर सकता, इसलिए मैं नीचे "उत्तर" जोड़ूंगा। – jasper77