0
当我使用两个子进程(我将添加更多,当我得到这个固定)打印到文件时,我目前有一个问题。我从awk中得到了总和,现在我试图将它打印到一个文件中,但它不能正确打印。我使用的是Python 2.6,无法升级。我也在我的程序结束时关闭了我的文件,所以这不是问题。 编辑: 此代码的目的是循环遍历文件并计算特定字符串出现的次数。将它们放在文件中,然后将文件总计在最终的输出文件中。多个子进程打印到文件
def serv2Process(HOST = "testServer.com"):
encInTotal1 = 0
signInTotal1 = 0
p = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
shell = False,
stdout = subprocess.PIPE,
close_fds = True)
for line in p.stdout:
if 'String to search'.lower() in line.lower():
totalCount1 = totalCount1 +1
if 'String 2 to search'.lower() in line.lower():
totalCount2 = totalCount2 +1
file1.write("%s\n" %totalCount1)
file2.write("%s\n" %totalCount2)
sys.stdout.flush()
p1 =threading.Thread(target = serv1Process, args=(HOST1,), name = 'serv1Process')
p2=threading.Thread(target = serv2Process, args= (HOST2,), name = 'serv2Process')
p1.start()
p2.start()
p1.join()
p2.join()
with open("SomeScriptName.%s" % strDateForm, 'w+')as search_file:
search_file.write("Header: ")
sys.stdout.flush()
proc = subprocess.Popen(['awk', '{sum+=$1} END {print sum}',"file1.%s" %strDateForm], shell=False, stdout=search_file,close_fds = True)
proc.wait()
search_file.write("\n")
search_file.write("Header2: ")
search_file.flush()
proc2 = subprocess.Popen(['awk', '{s+=$1} END {print s}',"file2.%s" %strDateForm], shell=False, stdout=search_file,close_fds = True)
proc2.wait()
file1.close()
file2.close()
这只是将头文件打印到文件中,而不是进程的结果。 – cvirus96
你确定你的awk程序产生任何输出吗?按照我的建议尝试删除'stderr'重定向,也许你会从awk过程中获得一些错误。 – mata
是的。当我用一个输出来运行它时,它工作得很好。但只要我在其中投入另一个子进程不起作用 – cvirus96