问题描述
我有一个包含其他目录的目录(目录的数目是任意的),例如:
I have a directory that contains other directories (the number of directories is arbitrary), like this:
Main_directory_samples /
Main_directory_samples/
- subdirectory_sample_1 /
- subdirectory_sample_2 /
- subdirectory_sample_3 /
- subdirectory_sample_4 /
- subdirectory_sample_1/
- subdirectory_sample_2/
- subdirectory_sample_3/
- subdirectory_sample_4/
我有一个脚本,该脚本每次接收一个目录作为输入,并且运行1h(对于每个目录)。要运行脚本,我需要以下代码:
I have a script that receives as input one directory each time and it takes 1h to run (for each directory). To run the script I have the following code:
opendir DIR, $maindirectory or die "Can't open directory!!";
while(my $dir = readdir DIR){
if($dir ne '.' && $dir ne '..'){
system("/bin/bash", "my_script.sh", $maindirectory.'/'.$dir);
}
}
closedir DIR;
但是,我想同时为不同目录运行脚本。例如, subdirectory_sample_1 /
和 subdirectory_sample_2 /
将在同一线程中运行; subdirectory_sample_3 /
和 subdirectory_sample_4 /
在另一个目录中。但我只是找不到办法。
However, I want to run the script for different directories at the same time. For instance, the subdirectory_sample_1/
and subdirectory_sample_2/
would run in the same thread; subdirectory_sample_3/
and subdirectory_sample_4/
in another. But I just can't find a way to do this.
推荐答案
当您刚刚启动外部进程并等待它们时,一个非线程选择:
As you're just starting external processes and waiting for them, a non-threading option:
use strict;
use warnings;
use Path::Tiny;
use IO::Async::Loop;
use Future::Utils 'fmap_concat';
my $loop = IO::Async::Loop->new;
my $maindirectory = '/foo/bar';
my @subdirs = grep { -d } path($maindirectory)->children; # excludes . and ..
# runs this code to maintain up to 'concurrent' pending futures at once
my $main_future = fmap_concat {
my $dir = shift;
my $future = $loop->new_future;
my $process = $loop->open_process(
command => ['/bin/bash', 'my_script.sh', $dir],
on_finish => sub { $future->done(@_) },
on_exception => sub { $future->fail(@_) },
);
return $future;
} foreach => \@subdirs, concurrent => 2;
# run event loop until all futures are done or one fails, throw exception on failure
my @exit_codes = $main_future->get;
请参阅和。
这篇关于Perl同时为不同的目录运行相同的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!