【发布时间】:2017-03-18 22:53:03
【问题描述】:
所以我有一个 perl 脚本可以输出并获取流的片段(我不知道有多少片段)
但我想不出一个知道何时停止工作的好方法。现在如果 wget 返回不成功,那么我们创建一个名为“end”的文件,一旦主程序看到它,它就会停止循环。有没有更好的方法来做到这一点?
显然,如果它是按顺序完成而不是同时完成会很容易,但我试图让它下载速度最快。
my $link = $ARGV[0];
my ($url) = $link=~ m/(.+-)\d+.ts/i;
my $num = 0;
#while the file END doesn't exist
my @pids;
while (! -e "END") {
#create the URL, increment by 1
my $video=$url.++$num.".ts";
die "could not fork" unless defined (my $pid = fork());
#child process goes until wget returns invalid, create END
if (not $pid) {
system ("wget -T 5 -t 5 $video");
`touch END` if $? != 0;
exit;
}
push @pids, $pid;
}
#parent process still running, waiting for the same END file.
for my $pid (@pids) { waitpid $pid,0; }
print "pids finished\n";
sleep 1;
`rm END`;
【问题讨论】: