在 python 中顺序执行子流程
Execute subprocess sequentially in python
我正在尝试在 python 中依次执行以下 2 个命令。
runmqsc <Queuem manager name>
Display QL (<queue name>)
我可以使用子进程执行 rumqsc 命令。
subprocess.call("runmqsc <queue manager name>", shell= True)
现在这个命令似乎是从 python 手中夺取了控制权。如果我尝试使用子进程执行下一个命令,它不会按预期工作。
我什至不确定如何执行第二个(我必须为此传递一个参数)。
添加代码片段:
subprocess.call("runmqsc Qmgrname", shell= True)
subprocess.call("DISPLAY QL(<quename>)",shell=True)
现在第一行执行正常,正如 tdelaney 在评论中提到的,runmqsc 等待来自标准输入的输入。并且在执行完第一行之后程序甚至没有执行第二行就挂了。
任何帮助或对任何相关文档的引用都会有所帮助。
谢谢
在 Unix 上,Linux 或 Windows,您可以简单地执行:
runmqsc QMgrName < some_mq_cmds.mqsc > some_mq_cmds.out
在 'some_mq_cmds.mqsc' 文件中,将您的 MQSC 命令放入:
DISPLAY QL("TEST.Q1")
您不想 运行 按顺序子处理命令。当您在命令行上 运行 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
。它应该执行命令并退出。结果是 Popen.communicate
为你做了这件事。我不知道你是否想捕获输出,但在这个例子中我让它进入屏幕。
# 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中,out
和err
是bytes
对象,不是字符串。与读取文本文件时使用编码类似,您可能必须根据程序使用的任何语言对它们进行解码。假设文件是 UTF8,那么你会
out = out.decode('utf-8')
我正在尝试在 python 中依次执行以下 2 个命令。
runmqsc <Queuem manager name>
Display QL (<queue name>)
我可以使用子进程执行 rumqsc 命令。
subprocess.call("runmqsc <queue manager name>", shell= True)
现在这个命令似乎是从 python 手中夺取了控制权。如果我尝试使用子进程执行下一个命令,它不会按预期工作。 我什至不确定如何执行第二个(我必须为此传递一个参数)。
添加代码片段:
subprocess.call("runmqsc Qmgrname", shell= True)
subprocess.call("DISPLAY QL(<quename>)",shell=True)
现在第一行执行正常,正如 tdelaney 在评论中提到的,runmqsc 等待来自标准输入的输入。并且在执行完第一行之后程序甚至没有执行第二行就挂了。
任何帮助或对任何相关文档的引用都会有所帮助。 谢谢
在 Unix 上,Linux 或 Windows,您可以简单地执行:
runmqsc QMgrName < some_mq_cmds.mqsc > some_mq_cmds.out
在 'some_mq_cmds.mqsc' 文件中,将您的 MQSC 命令放入:
DISPLAY QL("TEST.Q1")
您不想 运行 按顺序子处理命令。当您在命令行上 运行 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
。它应该执行命令并退出。结果是 Popen.communicate
为你做了这件事。我不知道你是否想捕获输出,但在这个例子中我让它进入屏幕。
# 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中,out
和err
是bytes
对象,不是字符串。与读取文本文件时使用编码类似,您可能必须根据程序使用的任何语言对它们进行解码。假设文件是 UTF8,那么你会
out = out.decode('utf-8')