Python:将子进程Popen stdout重定向到日志文件
我有一个python进程正在运行,配置了一个logger对象来打印日志文件中的日志 现在,我正试图通过这个Python:将子进程Popen stdout重定向到日志文件,python,scala,logging,subprocess,Python,Scala,Logging,Subprocess,我有一个python进程正在运行,配置了一个logger对象来打印日志文件中的日志 现在,我正试图通过这个python过程调用scala脚本,方法是使用python的subprocess模块 subprocess.Popen(scala_run_command, stdout=subprocess.PIPE, shell=True) 问题是,每当python进程退出时,它都会挂起shell,只有在显式运行sttysane命令后,shell才会生效。我猜这是因为scala脚本输出到shell,因
python
过程调用scala
脚本,方法是使用python的subprocess
模块
subprocess.Popen(scala_run_command, stdout=subprocess.PIPE, shell=True)
问题是,每当python进程退出时,它都会挂起shell,只有在显式运行sttysane
命令后,shell才会生效。我猜这是因为scala脚本输出到shell,因此shell挂起,因为它的stdout[其stdout中的某些内容导致shell失去理智]
出于同样的原因,我想尝试将要捕获的scala运行脚本的输出放在我的默认日志文件中,这似乎不是通过多种方式实现的
因此,查询归结为,如何通过日志文件中的子流程
模块获取shell命令的stdout
输出。即使有更好的方法来实现这一点,而不是子流程,请运行,我也很想知道这些想法
代码的当前状态如下所示
__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)
使用
接
stdout, stderr = p.communicate()
然后stdout
和stderr
将包含来自子流程输出流的输出字节。然后可以记录标准输出值。使用
接
stdout, stderr = p.communicate()
然后stdout
和stderr
将包含来自子流程输出流的输出字节。然后,您可以记录stdout
值。尝试删除shell=True
?即使删除此参数,它也不会运行,FileNotFoundError失败。您应该删除shell=True
并调试FileNotFoundError
。您正在传递可执行文件的完整路径吗?向我们显示命令的实际内容。尝试删除shell=True
?如果删除此参数,它甚至不会运行,FileNotFoundError失败。您应该删除shell=True
并调试FileNotFoundError
。您正在传递可执行文件的完整路径吗?向我们显示您命令的实际内容。