从另一个python脚本调用面向命令行的脚本 [英] Call command-line oriented script from another python script

查看:64
本文介绍了从另一个python脚本调用面向命令行的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python编写的脚本,该脚本使用argparse模块从命令行获取其参数。我尝试尽可能少地修改此文件,因为各种各样的人都在处理它。

I am using a script written in Python that uses the argparse module to get it's arguments from command line. I try to modify this file as less as possible as various people work on it.

例如:脚本称为CLscript.py,我用

Ex: script is called CLscript.py and I call it with

python CLscript.py -option1 arg1 -flag1 -option2 arg2

但是我面对的情况是,我想将事情自动化到更高水平,并使用大量脚本生成的参数自动启动此脚本。

But I'm facing the case where I would like to automate things one level higher and automatically launch this script with wide range of script-generated arguments.

我想继续使用此脚本中所有现有的选项和标志组织。

I would like to keep using all the existing organisation of options and flags available in this script.

例如,当我从topLevelScript.py通过以下方式运行CLscript.py时:

For example, when I run CLscript.py from topLevelScript.py through :

subprocess.call("python CLscript.py -option1 arg1 -flag1 -option2 arg2")

,然后从输出中看到出了点问题,我停止了执行topLevelScript.py,但是CLscript.py继续在另一个我必须手动终止的python进程中独立运行。我既不能在调试模式下启动topLevelScript.py也不能在CLscript.py的断点处停止。

,and I see from the output that something is going wrong, I stop the execution topLevelScript.py, but CLscript.py continue to run independently in another python process that I Have to kill manually. I cannot neither launch topLevelScript.py in debug mode to stop at a breakpoint in CLscript.py.

我想从python内部完成所有操作,而无需构建命令行字符串,并使用子进程启动CLscript.py。
每个调用都将附加到相同的原始启动上,就像函数调用一样,并且不会像subprocess.call()那样创建多个python线程。

I would like to do this all from inside python, without building a command line string and launching CLscript.py with a subprocess. Every call would remains attached to the same original launch, just like a function call, and not creating multiple python threads like it would do with a subprocess.call() .

像是将带有选项,标志和参数的字符串列表传递给脚本一样?

Something like passing a list of string with options, flags and arguments somehow to the script maybe?

是像这样的

import CLscript.py
CLsimulator(CLscript,["-option1",arg1,"-flag1","-option2",arg2])


推荐答案

第一个全部使用 http://docs.python.org/2/library /subprocess.html#subprocess.Popen 而不是 subprocess.call()

First of all, use http://docs.python.org/2/library/subprocess.html#subprocess.Popen and not subprocess.call():

import subprocess

child = subprocess.Popen(
    ['python', 'CLscript.py', '-option1', 'arg1', '-flag1', '-option2', 'arg2'],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

请注意,作为第一个参数,您传递了字符串数组就像您想要的一样。

其次是重定向标准文件描述符将很重要。请参见 http://docs.python.org/2/library/subprocess。 html#subprocess.PIPE

现在,您有 child 变量,其中包含 Popen 类。

这个实例可以做什么?

Please notice that as the first argument you pass an array of strings just like you want.
Secondly redirection of standard file descriptors will be important. See http://docs.python.org/2/library/subprocess.html#subprocess.PIPE.
Now you have child variable which holds instance of Popen class.
What you can do with this instance?

# Check if child is terminated and possibly get the returncode
child.poll()
# Write to child's standard input by a file-like object accessible with
child.stdin
# Read child's standard output and standard error by file-like objects accesible with
child.stdout
child.stderr

您说您想从子进程的输出中检测出子进程是否出了问题。 br>
在这种情况下,您发现 stdout stderr 有用吗?

现在,如果您检测到出了什么问题,就想终止孩子。

You said you wanted to detect if something goes wrong in the child process from it's output.
Don't you find stdout and stderr quite useful for this case?
Now you wanted to terminate the child if you detected that something went wrong.

child.kill()
child.terminate()
child.send_signal(signal)

如果最后你确保一切顺利,但是您想让孩子正常完成工作,应该使用

If in the end you are sure that everything went well but you want to let child finalize normally, you should use

child.wait()

甚至更好

child.communicate()

因为进行通信将正确处理大量输出。

because communicate will handle lots of output properly.

祝你好运!

这篇关于从另一个python脚本调用面向命令行的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆