4

現在、特定の時点でいくつかの bash コマンドの出力を収集して処理する perl スクリプトがあります。

if ($condition) {
  @output = `$bashcommand`;
  @output1 = `$bashcommand1`;
  @output2 = `$bashcommand2`;
  @output3 = `$bashcommand3`;
}

問題は、これらのコマンドのそれぞれにかなりの時間がかかることです。そのため、これらすべてを同時に実行できるかどうかを知りたいのです。

4

4 に答える 4

4

Unix システムでは、複数のコマンド パイプを開きIO::Select、それらのいずれかが読み取り可能になるまで待機するループ呼び出しを実行できるはずです。sysreadそれらがすべてファイルの終わりに達するまで、出力を読み取り、丸呑みし続けます(を使用)。

残念ながら、Unix の Win32 エミュレーションはselectファイル I/O を処理できないようです。そのため、Windows でそれを実行するには、ソケット I/O の層も追加する必要があります。これについてはperlmonksselectを参照してください。

于 2012-02-06T19:44:56.273 に答える
3

これは の良い使用例のように思えますForks::Super::bg_qx

use Forks::Super 'bg_qx';
$output = bg_qx $bashcommand;
$output1 = bg_qx $bashcommand1;
$output2 = bg_qx $bashcommand2;
$output3 = bg_qx $bashcommand3;

これら 4 つのコマンドをバックグラウンドで実行します。戻り値 ( 、 など) に使用される変数$output$output1、オーバーロードされたオブジェクトです。プログラムは、次にこれらの変数がプログラムで参照されるときに、これらのコマンドからの出力を取得します (必要に応じて、コマンドが完了するのを待ちます)。

... more stuff happens ...
# if $bashcommand is done, this next line will execute right away
# otherwise, it will wait until $bashcommand finishes ...
print "Output of first command was ", $output;

&do_something_with_command_output( $output1 );
@output2 = split /\n/, $output2;
...

2012-03-01 更新: Forks::Super の v0.60 には、リスト コンテキストで結果を取得できるいくつかの新しい構造があります。

if ($condition) {
    tie @output, 'Forks::Super::bg_qx', $bashcommand;
    tie @output1, 'Forks::Super::bg_qx', $bashcommand1;
    tie @output2, 'Forks::Super::bg_qx', $bashcommand2;
    tie @output3, 'Forks::Super::bg_qx', $bashcommand3;
}
...
于 2012-02-06T20:41:36.797 に答える
2

できますが、バックティックは使用できません。

代わりに、 を使用して実際のファイル ハンドルを開きopen(handle, "$bashcommand|");、適切なselect呼び出しを行って、準備ができている新しい出力があるファイルを特定する必要があります。上記の 6 行よりもはるかに多くの時間がかかりますが、それらすべてを同時に実行することができます。

CPANには、複雑さの一部を既に管理しているクラスがいくつかあります。

于 2012-02-06T19:37:56.753 に答える
0

Perl FAQを参照してください。

Proc::Backgroundは有望に見えます。

于 2012-02-06T19:40:32.997 に答える