我正在使用Python使用不同的输入运行较长的(4-60分钟的运行时间)Perl程序。
Perl程序较旧,并且使用警告已注释掉。不同的输入使用代码的不同部分。
# use warnings FATAL => 'all';
从bash运行CMD时,所有输入都起作用!
perl -I/storage my_script_entance.pl --ex_config R....
使用Popen运行它们时,某些输入会崩溃:
run_script('perl -I/storage my_script_entance.pl --ex_config R....')
def run_script(cmd):
args = shlex.split(f'{cmd}')
# p = sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE)
p = sp.Popen(args, stderr=sp.PIPE)
output, err = p.communicate()
print(f'output: {output}')
print(f'err: {err}')
rc = p.returncode
print(f"script return code: {rc}")
崩溃看起来像这样:
output: None
err: b''
script return code: -9
它们出现在代码中的许多不同位置
我的环境是:
具有Alpine Docker容器的Kubernetes具有:
为什么从Popen运行脚本与从bash运行脚本不同?
有没有一种方法可以相同地运行它们。
我阅读了Popen文档,并尝试通过example将运行功能与check = False一起使用。但无法读取输出错误管道。
我使用pexpect尝试了一些选项,但到目前为止失败了。
有没有一种方法可以像在bash中一样运行脚本并仅打印标准输出和错误?
压力
我使用Python启动Perl的原因是由于高级的Kafka库(Perl Kafka库是片状且僵化的)
最佳答案
好吧@ikegami给我的问题很复杂:
args = ['bash', '-c', cmd]
由perl和Bash创建缓冲区。 所以我问了一个 friend ,我们提出了两个解决方案,即:
wait()
并忽略阻塞标准错误后访问标准输出。 两者仅在Perl设置为立即刷新输出时才起作用:
$| = 1;
这是一个示例perl脚本:
#!/usr/bin/perl -w
use strict;
# use warnings FATAL => 'all';
use feature qw/say/;
use Try::Tiny;
use File::Basename;
use Data::Dumper;
use Getopt::Long;
use Data::Dumper;
use YAML::XS 'LoadFile';
use JSON::MaybeXS qw(encode_json decode_json);
use JSON qw();
$| = 1;
say("Abra dabra");
my $noise = 'Mooo';
# my $bot_init_args;
GetOptions(
'noise=s' => \$noise,
# 'bot_init_args=s' => \$bot_init_args
);
if (defined $noise) {
my $limit = 5;
print STDERR "something awful\n";
for(my $i=0; $i < $limit; $i++){
say("$i of $limit: $noise");
sleep(2);
}
print STDERR "something embarrassing\n";
sleep(2);
exit 0;
}
这是Python 3.82中的两个解决方案:
#!/usr/bin/env python
import shlex
import subprocess as sp
import selectors
def read_stdo(something):
print('standard output')
print(f'{type(something)}')
print(something.decode("utf-8"))
def read_stderr(something):
print('standard error')
print(f'{type(something)}')
print(something.decode("utf-8"))
def channel(cmd):
args = shlex.split(cmd)
sel = selectors.DefaultSelector()
p = sp.Popen(args=args, stderr=sp.PIPE, stdout=sp.PIPE, bufsize=0)
sel.register(p.stdout, selectors.EVENT_READ, data=read_stdo)
sel.register(p.stderr, selectors.EVENT_READ, data=read_stderr)
while p.poll() is None:
print(p.poll())
events = sel.select()
for key, mask in events:
line = key.fileobj.readline()
print(f'{type(line)}')
key.data(line)
return_val = p.wait()
print(f'return val: {return_val}')
# return lines
def run_script(cmd):
print("Reading command Standard Output without listening to Standard Error")
args = shlex.split(f'{cmd}')
print(f'cmd: {cmd}')
print(args)
# p = sp.Popen(args=['/bin/bash', '-c', 'printenv;', ' sleep 25;', ' pwd'], stdout=sp.PIPE)
p = sp.Popen(args=args, stderr=sp.PIPE, stdout=sp.PIPE, bufsize=0)
line = p.stdout.readline()
while line:
print(line.decode("utf-8"))
line = p.stdout.readline()
# p = sp.run(['bash', '-c', cmd], stdout=sp.PIPE, stderr=sp.PIPE)
# output, err = p.communicate()
# print(f'output: {output.decode("utf-8")}\n')
# if err is not None:
# print(f'err: {err.decode("utf-8")}')
p.wait()
rc = p.returncode
print(f"script return code: {rc}")
if __name__ == "__main__":
_cmd = 'perl -I/storage /storage/annoyingPerl.pl --noise quack'
run_script(_cmd)
channel(f'{_cmd} barf')
关于python-3.x - 如何从Python脚本运行Perl程序,同时接收日志并避免脚本返回代码崩溃:-9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61162092/