简体   繁体   English

从单独的窗口python获取子进程的输出

[英]Getting an output from a subprocess from a separate window python

I am trying to write a program which lets the user select a variety of options which will contribute to a command that will run in a separate window in python. 我正在尝试编写一个程序,让用户选择各种选项,这些选项将有助于在python的单独窗口中运行的命令。 After this sub-process finishes, I would like it to exit and return whatever the output is for that particular command. 在这个子进程完成之后,我希望它退出并返回该特定命令的输出。 Here is a snippet of my code: 这是我的代码片段:

if(fileName == "" or className == ""):
    tkMessageBox.showinfo("Error", "Please select a test class/test!")
else:
    command = "ctetest"
    if(verbose.get()):
        command += " -v"
    if(xml.get()):
        command += " -x"
    if(version.get()):
        command += " -V"
    if(output.get()):
        command += " -o"
    command += " RegressionTest/"
    command += folderName + " " + fileName + "." + className + "." + methodName
    processOutput = subprocess.Popen('start '+command, shell = True)
    processOutput.wait()
    processOutput.terminate()

The problem is that when the subprocess finishes, it just hangs there and I have to close it manually. 问题是,当子进程完成时,它只是挂起,我必须手动关闭它。 Once I close it manually, it does not seem to generate any of the output produced by the subprocess. 一旦我手动关闭它,它似乎不会生成子进程产生的任何输出。 I have tried using 我试过用

subprocess.check_output('start '+command, shell = True)

as well as 以及

subprocess.popen('start '+command+' & exit', shell=True)

But same issue arises. 但同样的问题出现了。 The only work around I can think of is using threads but then how could I check if the subprocess is actually finish before I terminate it? 我能想到的唯一工作就是使用线程,但是如何在终止之前检查子进程是否实际完成? Are there any other ways that I can accomplish this? 还有其他方法可以实现这个目标吗? Thanks in advance! 提前致谢!

subprocess needs you to pass the arguments differently than what you're doing, you need to create a list that consists of the program to be run and it's arguments. subprocess需要您以不同于您正在执行的操作的方式传递参数,您需要创建一个包含要运行的程序及其参数的列表。 For example ["ls", "-l", "/home"] . 例如["ls", "-l", "/home"]

So, your code would need to be this: 所以,你的代码需要是这样的:

if(fileName == "" or className == ""):
    tkMessageBox.showinfo("Error", "Please select a test class/test!")
else:
    command = ["start ctetest"]
    if(verbose.get()):
        command.append("-v")
    if(xml.get()):
        command.append("-x")
    if(version.get()):
        command.append("-V")
    if(output.get()):
        command.append("-o")
    commandpath = "RegressionTest/" + folderName + " " + fileName + "." + className + "." + methodName
    command.append(commandpath)
    processOutput = subprocess.check_output(command, shell=True)

This creates the list of command and arguments with list.append() . 这将使用list.append()创建命令和参数列表。 It will automatically wait for the subprocess to finish and provide the entire output in the processOutput variable. 它将自动等待子processOutput完成并在processOutput变量中提供整个输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM