我执行模块popen1.py并使用子进程模块
调用popen2.py但popen2.py的输出没有显示..当我显示子进程ID时,它正在显示..输出将打印为popen2.py
呼叫
child = subprocess.Popen(['python', 'popen2.py',"parm1='test'","parm='test1'"], shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
答案 0 :(得分:1)
完成此过程后,您可以阅读child.stdout
和child.stderr
来获取数据(因为您通过了subprocess.PIPE
)
或者,您可以使用oudata,errdata = child.communicate()
等待子流程完成,然后将其输出为字符串。
从设计角度来看,最好导入。我会按如下方式重构popen2.py
:
#popen2.py
# ... stuff here
def run(*argv):
#...
if __name__ == '__main__':
import sys
run(sys.argv[1:])
然后你可以在popen1.py中导入并运行popen2.py:
#popen1.py
import popen2
popen2.run("parm1=test","parm=test1")
答案 1 :(得分:0)
您可以使用child.stdout / child.stdin与进程通信:
child = subprocess.Popen(['python', 'popen2.py',"parm1='test'","parm='test1'"], shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print child.stdout.readlines()