2012-10-23 33 views
0

林执行的模块和popen1.py的呼叫popen2.py使用子模块,通过POPEN执行的模块

但popen2.py的输出是不是被displayed..When我显示子进程id ,它是displayed..Where输出是否会被打印,popen2.py

呼叫

child = subprocess.Popen(['python', 'popen2.py',"parm1='test'","parm='test1'"], shell=True, 
         stdin=subprocess.PIPE, 
         stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
+0

如果你正在从python执行python代码,为什么不''execfile'或'import'?是真正必需的第二个过程 – tMC

+0

@mgilson谢谢 – tMC

+0

@tmc:我可以使用execfile传递参数吗 – user1050619

回答

1

过程完成后,你可以阅读child.stdoutchild.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") 
0

您可以使用child.stdout/child.stdin与以comunicate进程:

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() 
+0

输出为空白。我怎么知道模块是否在第一位被调用..我没有看到我的代码中的任何错误 – user1050619