Python:将子进程Popen stdout重定向到日志文件 [英] Python : Redirecting subprocess Popen stdout to log file

查看:427
本文介绍了Python:将子进程Popen stdout重定向到日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的python进程,具有配置为在日志文件中打印日志的logger对象.

I have a python process running, having a logger object configured to print logs in a log file.

现在,我正在尝试通过使用Python的subprocess模块通过此python进程调用scala脚本.

Now, I am trying to call a scala script through this python process, by using subprocess module of Python.

subprocess.Popen(scala_run_command, stdout=subprocess.PIPE, shell=True)

问题是,每当python进程退出时,它就会挂起shell,只有明确运行stty sane命令后,shell才能生效.我的猜测是,它是由于scala脚本输出到shell并因此由于其stdout而挂起的([stdout中的某些内容导致shell失去了理智].

The issue is, whenever the python process exits, it hangs the shell, which comes to life only after explicitly running stty sane command. My guess is that it is caused because the scala script outputs to shell and hence the shell hangs, because of its stdout [something in its stdout causes the shell to lose its sanity].

出于相同的原因,我想尝试将scala运行脚本的输出捕获到我的默认日志文件中,这似乎并没有通过多种方式发生.

For the same reason, I wanted to try to put the output of scala run script to be captured in my default log file, which does not seem to be happening using multiple ways.

因此,查询归结为,如何通过日志文件中的subprocess模块运行shell命令的stdout输出.即使有更好的方法可以代替subprocess, run来实现此目标,我也很想知道这些想法.

So, the query boils down to, how to the get the stdout output of shell command ran through subprocess module in a log file. Even if there is a better way to achieve this instead of subprocess, run, I would love to know the ideas.

代码的当前状态如下.

__echo_command = 'echo ":load %s"' 
__spark_console_command = 'spark;' 
def run_scala_script(self, script): 
     echo_command = self.__echo_command % script 
     spark_console_command = self.__spark_console_command 
     echo_result = subprocess.run(echo_command, stdout=subprocess.PIPE, shell=True) 
     result = subprocess.run(spark_console_command, stdout=subprocess.PIPE, shell=True, input=echo_result.stdout) 
     logger.info('Scala script %s completed successfully' % script) 
     logger.info(result.stdout)

推荐答案

使用

p = subprocess.Popen(...)

之后

stdout, stderr = p.communicate()

,然后stdoutstderr将包含子进程的输出流的输出字节.然后,您可以记录stdout值.

and then stdout and stderr will contain the output bytes from the subprocess' output streams. You can then log the stdout value.

这篇关于Python:将子进程Popen stdout重定向到日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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