非阻塞子进程阻止文件

时间:2017-05-22 12:40:24

标签: perl

考虑这种情况:

我们有三个脚本:

script.pl

use strict;
use warnings;


print "\nStarting a blocking process";
print "\nRedirect the output of the blocking process to execution.log";
my $cmd = "perl d:\\blocking_script.pl >d:\\execution.log";
my $exitCode = system ($cmd);
print "\nAfter the execution of the blocking process";

print "\nNow I try to rename the log";
rename "d:\\execution.log", "d:\\execution.err" or print "\nCouldn't rename because : $!";

blocking_script.pl

use strict;
use warnings;

print "\nFrom the blocking_process I run a non-blocking process";
my $cmd = "start perl d:\\non_blocking_script.pl";
my $exitCode = system ($cmd);
print "\nAfter I started the non-blocking process";

non_blocking_script.pl

use strict;
use warnings;

print "\nI am an independent non-blocking process";
sleep 5;
print "\nStill here";
sleep 2;
print "\nYou can't rename the log because you didn't wait for me";
sleep 3;
print "\n.";
sleep 1;

这会产生什么结果?

Couldn't rename because : Permission denied

虽然另一个命令promopt将具有讽刺意味:

I am an independent non-blocking process
Still here
You can't rename the log because you didn't wait for me
.

在我的perl情况下,我以阻塞方式运行外部应用程序,但该应用程序正在启动一些非阻塞进程,这些进程持有我的日志。

我如何克服这种情况?

1 个答案:

答案 0 :(得分:1)

这是documentation for start(你也应该可以在命令行中使用start /?来阅读。我现在无法访问Windows系统,因此我无法验证

  

/b

     

启动应用程序而不打开新的命令提示符窗口。除非应用程序启用 CTRL + C 处理,否则忽略 CTRL + C 处理。使用 CTRL + BREAK 来中断应用程序。

blocking_script.pl正在等待start打开以运行non_blocking_script.pl的cmd窗口。

在短期内,使用start /b可能有所帮助。

或者,您可以尝试

my @cmd = start => qw(perl d:\\non_blocking_script.pl);
my $exitCode = system @cmd;

但是,您应该change your design