在python中按顺序执行子进程

2024-10-01 00:26:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在python中一个接一个地使用以下两个命令。在

runmqsc <Queuem manager name>
Display QL (<queue name>)

我可以使用子进程执行rumqsc命令。在

^{pr2}$

现在这些命令似乎从python获得了控制权。如果我试图使用子进程执行下一个命令,它将无法按预期工作。 我甚至不知道如何执行第二个(我必须传递一个参数)。在

添加代码段:

subprocess.call("runmqsc Qmgrname", shell= True)
subprocess.call("DISPLAY QL(<quename>)",shell=True)

现在第一行执行得很好,正如tdelaney在runmqsc注释中所提到的,它等待来自stdin的输入。在执行第一行之后,程序甚至没有执行第二行就挂起了。在

任何帮助或对任何相关文档的引用都会有所帮助。 谢谢


Tags: name命令truequeue进程displaymanagershell
2条回答

您不希望按顺序运行到子进程命令。当您在命令行上运行runmqsc时,它接管stdin,执行您输入的命令,然后在您告诉它时退出。来自the docs

By taking stdin from the keyboard, you can enter MQSC commands interactively. By redirecting the input from a file, you can run a sequence of frequently used commands contained in the file. You can also redirect the output report to a file.

但我认为还有第三种方法。启动runmqsc,将命令写入stdin,然后关闭stdin。它应该执行命令并退出。原来^{}是为你做的。我不知道你是否想捕捉输出,但在这个例子中,我让它进入屏幕。在

# start msg queue manager
mqsc = subprocess.Popen(["runmqsc", "QMAGTRAQ01"], stdin=subprocess.PIPE,
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# pass command(s) to manager and capture the result
out, err = mqsc.communicate("DISPLAY QL(BP.10240.012.REQUEST)")
# wait for command to complete and deal with errors
retcode = mqsc.wait()
if retcode != 0:
    print("  ERROR  ") # fancy error handling here
print("OUTPUT: ", out)
print()
print("ERROR: ", err)

在python3中,outerrbytes对象,而不是字符串。与读取文本文件时使用编码类似,您可能必须根据程序使用的任何语言对其进行解码。假设文件是UTF8,那么你就可以了

^{pr2}$

在Unix、Linux或Windows上,您只需执行以下操作:

runmqsc QMgrName < some_mq_cmds.mqsc > some_mq_cmds.out

在“some\u mq”里_命令.mqsc'文件,将MQSC命令放在如下位置:

^{pr2}$

相关问题 更多 >