简体   繁体   English

从子进程调用python脚本不输出

[英]Calling python script from subprocess not outputting

I have code in two environments, a windows machine with a Linux VM. 我在两种环境中都有代码,一台带有Linux VM的Windows机器。 I'm trying to create a script that builds the code on the windows side via a script, buildWindows.py , run on the Linux side. 我正在尝试创建一个脚本,该脚本通过在Linux端运行的脚本buildWindows.py在Windows端构建代码。

I'm having the script ssh over to the windows side, and then run a python builder script windowsBuilder.py . 我将脚本ssh移到Windows端,然后运行python构建器脚本windowsBuilder.py

The issue I'm having is that the python builder script on the windows side takes a while to run, and nothing is being printed out until the program finishes. 我遇到的问题是Windows端的python builder脚本需要一段时间才能运行,并且在程序完成之前什么都没有打印出来。 Is there a way to have the output print while the script is running, versus when it ends? 有没有一种方法可以在脚本运行时和结束时打印输出?

I'm assuming I'm having this issue because when you call a python script, it opens a new python environment, and it only returns information once the python environment is finished. 我假设我遇到了这个问题,因为当您调用python脚本时,它将打开一个新的python环境,并且仅在python环境完成后才返回信息。

subprocess call in buildWindows.py : buildWindows.py子流程调用:

COMMAND = "cd C:/codeLocation/; python windowsBuilder.py"
ssh = subprocess.Popen(["ssh", "%s" % IP_ADDRESS, COMMAND],
                   shell=False,
                   stdout=subprocess.PIPE)
for line in iter(ssh.stdout.readline, ''):
    sys.stdout.write(line)

Turn off buffering: bufsize=0 parameter passed to call 关闭缓冲: bufsize=0参数传递给调用

 ssh = subprocess.call(["ssh", "%s" % IP_ADDRESS, COMMAND],
                   shell=False,
                   stdout=subprocess.PIPE,
                   bufsize=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM