5

我运行了一些 bash 脚本,但它们可能需要几个小时才能完成,在此期间它们会喷出下载速度、ETA 和类似信息。我需要在 perl 中捕获这些信息,但是我遇到了一个问题,我无法逐行读取输出(除非我遗漏了什么)。

任何帮助解决这个问题?

编辑:为了更好地解释这一点,我正在同时运行几个 bash 脚本,我希望将 gtk 与 perl 一起使用来生成方便的进度条。目前,我为每个希望运行的 bash 脚本运行 2 个线程,一个用于更新图形信息的主线程。它看起来像这样(尽可能减少):

  my $command1 = threads->create(\&runCmd, './bash1', \@out1);
  my $controll1 = threads->create(\&monitor, $command1, \@out1);
  my $command1 = threads->create(\&runCmd, 'bash2', \@out2);
  my $controll2 = threads->create(\&monitor, $command2, \@out2);

  sub runCmd{
     my $cmd = shift;
     my @bso = shift;
     @bso = `$cmd`
  }
  sub monitor{
     my $thrd = shift;
     my @bso = shift;
     my $line;
     while($thrd->is_running()){
       while($line = shift(@bso)){
         ## I check the line and do things with it here
       }
       ## update anything the script doesn't tell me here.
       sleep 1;# don't cripple the system polling data.
     }
     ## thread quit, so we remove the status bar and check if another script is in the queue, I'm omitting this here.
  }
4

7 回答 7

10

而不是线程和``,使用:

 open my $fh, '-|', 'some_program --with-options';

以这种方式打开几个文件句柄(您需要运行的程序数量一样多),然后使用IO::Select它们来轮询数据。

简单的例子。

假设我有一个看起来像这样的 shell 脚本:

=> cat test.sh
#!/bin/bash
for i in $( seq 1 5 )
do
    sleep 1
    echo "from $$ : $( date )"
done

它的输出可能如下所示:

=> ./test.sh
从 26513 开始:2009 年 8 月 7 日星期五 08:48:06 CEST
从 26513 开始:2009 年 8 月 7 日星期五 08:48:07 CEST
从 26513 开始:2009 年 8 月 7 日星期五 08:48:08 CEST
从 26513 开始:2009 年 8 月 7 日星期五 08:48:09 CEST
从 26513 开始:2009 年 8 月 7 日星期五 08:48:10 CEST

现在,让我们写一个multi-test.pl

#!/usr/bin/perl -w
use strict;
use IO::Select;

my $s = IO::Select->new();

for (1..2) {
    open my $fh, '-|', './test.sh';
    $s->add($fh);
}

while (my @readers = $s->can_read()) {
    for my $fh (@readers) {
        if (eof $fh) {
            $s->remove($fh);
            next;
        }
        my $l = <$fh>;
        print $l;
    }
}

如您所见,没有分叉,没有线程。这就是它的工作原理:

=> 时间 ./multi-test.pl
从 28596 开始:2009 年 8 月 7 日星期五 09:05:54 CEST
从 28599 开始:2009 年 8 月 7 日星期五 09:05:54 CEST
从 28596 开始:2009 年 8 月 7 日星期五 09:05:55 CEST
从 28599 开始:2009 年 8 月 7 日星期五 09:05:55 CEST
从 28596 开始:2009 年 8 月 7 日星期五 09:05:56 CEST
从 28599 开始:2009 年 8 月 7 日星期五 09:05:56 CEST
从 28596 开始:2009 年 8 月 7 日星期五 09:05:57 CEST
从 28599 开始:2009 年 8 月 7 日星期五 09:05:57 CEST
从 28596 开始:2009 年 8 月 7 日星期五 09:05:58 CEST
从 28599 开始:2009 年 8 月 7 日星期五 09:05:58 CEST

真实0m5.128s
用户 0m0.060s
系统 0m0.076s
于 2009-08-07T07:06:21.520 回答
3

反引号和 qx// 运算符都会阻塞,直到子进程完成。您需要在管道上打开 bash 脚本。如果您需要它们是非阻塞的,请将它们作为文件句柄打开,必要时使用 open2 或 open3,然后将句柄放入 select() 并等待它们变得可读。

我刚刚遇到了一个类似的问题——我有一个运行时间很长的进程(一项可以运行数周的服务),我用 qx//. 问题是这个程序的输出最终超出了内存限制(在我的架构上大约为 2.5G)。我通过在管道上打开子命令来解决它,然后只保存最后 1000 行输出。这样做时,我注意到 qx// 表单仅在命令完成后打印输出,但管道表单能够在发生时打印输出。

我手头没有代码,但如果你能等到明天,我会发布我所做的。

于 2009-08-06T00:01:00.000 回答
2

有关您可以做的几件事,请参阅perlipc (进程间通信)。管道打开和 IPC::Open3 很方便。

于 2009-08-07T03:23:27.927 回答
1

是的你可以。

while (<STDIN>) { print "Line: $_"; }

问题是某些应用程序不会逐行输出信息,而是更新一行直到完成。是你的情况吗?

于 2009-08-05T22:39:16.450 回答
1

这是用于显示进度条的 GTK2 代码。

#!/usr/bin/perl
use strict;
use warnings;

use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

my $window = Gtk2::Window->new('toplevel');
$window->set_resizable(TRUE);
$window->set_title("command runner");

my $vbox = Gtk2::VBox->new(FALSE, 5);
$vbox->set_border_width(10);
$window->add($vbox);
$vbox->show;

# Create a centering alignment object;
my $align = Gtk2::Alignment->new(0.5, 0.5, 0, 0);
$vbox->pack_start($align, FALSE, FALSE, 5);
$align->show;

# Create the Gtk2::ProgressBar and attach it to the window reference.
my $pbar = Gtk2::ProgressBar->new;
$window->{pbar} = $pbar;
$align->add($pbar);
$pbar->show;

# Add a button to exit the program.
my $runbutton = Gtk2::Button->new("Run");
$runbutton->signal_connect_swapped(clicked => \&runCommands, $window);
$vbox->pack_start($runbutton, FALSE, FALSE, 0);

# This makes it so the button is the default.
$runbutton->can_default(TRUE);

# This grabs this button to be the default button. Simply hitting the "Enter"
# key will cause this button to activate.
$runbutton->grab_default;
$runbutton->show;

# Add a button to exit the program.
my $closebutton = Gtk2::Button->new("Close");
$closebutton->signal_connect_swapped(clicked => sub { $_[0]->destroy;Gtk2->main_quit; }, $window);
$vbox->pack_start($closebutton, FALSE, FALSE, 0);

$closebutton->show;

$window->show;

Gtk2->main;

sub pbar_increment {
    my ($pbar, $amount) = @_;

    # Calculate the value of the progress bar using the
    # value range set in the adjustment object
    my $new_val = $pbar->get_fraction() + $amount;

    $new_val = 0.0 if $new_val > 1.0;

    # Set the new value
    $pbar->set_fraction($new_val);
}

sub runCommands {
        use IO::Select;

        my $s = IO::Select->new();

        for (1..2) {
            open my $fh, '-|', './test.sh';
            $s->add($fh);
        }

        while (my @readers = $s->can_read()) {
            for my $fh (@readers) {
                if (eof $fh) {
                    $s->remove($fh);
                    next;
                }
                my $l = <$fh>;
                print $l;
                pbar_increment($pbar, .25) if $l =~ /output/;
            }
        }
    }

有关更多信息,请参阅perl GTK2 文档

于 2009-08-06T00:26:36.633 回答
1

我使用这个子例程和方法来记录我的外部命令。它是这样称呼的:

open($logFileHandle, "mylogfile.log");

logProcess($logFileHandle, "ls -lsaF", 1, 0); #any system command works

close($logFileHandle);

以下是子程序:

#******************************************************************************
# Sub-routine: logProcess()
#      Author: Ron Savage
#        Date: 10/31/2006
# 
# Description:
# This sub-routine runs the command sent to it and writes all the output from
# the process to the log.
#******************************************************************************
sub logProcess
   {
   my $results;

   my ( $logFileHandle, $cmd, $print_flag, $no_time_flag ) = @_;
   my $logMsg;
   my $debug = 0;

   if ( $debug ) { logMsg($logFileHandle,"Opening command: [$cmd]", $print_flag, $no_time_flag); }
   if ( open( $results, "$cmd |") )
      {
      while (<$results>)
         {
         chomp;
         if ( $debug ) { logMsg($logFileHandle,"Reading from command: [$_]", $print_flag, $no_time_flag); }
         logMsg($logFileHandle, $_, $print_flag, $no_time_flag);
         }

      if ( $debug ) { logMsg($logFileHandle,"closing command.", $print_flag, $no_time_flag); }
      close($results);
      }
   else
      {
      logMsg($logFileHandle, "Couldn't open command: [$cmd].")
      }
   }

#******************************************************************************
# Sub-routine: logMsg()
#      Author: Ron Savage
#        Date: 10/31/2006
# 
# Description:
# This sub-routine prints the msg and logs it to the log file during the 
# install process.
#******************************************************************************
sub logMsg
   {
   my ( $logFileHandle, $msg, $print_flag, $time_flag ) = @_;
   if ( !defined($print_flag) ) { $print_flag = 1; }
   if ( !defined($time_flag) ) { $time_flag = 1; }

   my $logMsg;

   if ( $time_flag ) 
      { $logMsg = "[" . timeStamp() . "] $msg\n"; }
   else 
      { $logMsg = "$msg\n"; } 

   if ( defined($logFileHandle)) { print $logFileHandle $logMsg; }

   if ( $print_flag ) { print $logMsg; }
   }
于 2009-08-06T00:45:36.460 回答
0

运行子进程并完全控制其输入和输出的最简单方法是IPC::Open2模块(或者IPC::Open3如果您也想捕获 STDERR),但是如果您想一次处理多个,或者特别是如果您想在 GUI 中执行此操作,正在阻塞。如果您只是进行<$fh>类型读取,它将阻塞,直到您输入,可能会楔入您的整个 UI。如果子进程是交互式的,那就更糟了,因为你很容易死锁,子进程和父进程都在等待对方的输入。您可以编写自己的select循环并进行非阻塞 I/O,但这并不值得。我的建议是使用POE,POE::Wheel::Run与子进程交互,并将POE::Loop::GtkPOE 包含到 GTK 运行循环中。

于 2009-08-08T11:26:37.817 回答