如何在 Perl 中以线程安全的方式更改当前目录?

发布于 2024-09-24 01:14:42 字数 654 浏览 5 评论 0原文

我正在使用 Thread::Pool::Simple 来创建一些工作线程。每个工作线程都会执行一些操作,包括调用 chdir,然后执行外部 Perl 脚本(从 jbrowse 基因组浏览器,如果重要的话)。我使用capturex调用外部脚本并因失败而死亡。

我发现当我使用多个线程时,事情就开始变得混乱。经过一番研究。似乎某些线程的当前目录不正确。

也许 chdir 在线程之间传播(即不是线程安全的)? 或者也许是与capturex有关?

那么,如何安全地设置每个线程的工作目录呢?

**更新**

按照执行时更改目录的建议,我想问一下我应该如何将这两个命令传递给capturex?

目前我有:

my @args = ( "bin/flatfile-to-json.pl", "--gff=$gff_file", "--tracklabel=$track_label", "--key=$key", @optional_args );
capturex( [0], @args );

如何向 @args 添加另一个命令? capturex 会继续因任何命令的错误而死亡吗?

I'm using Thread::Pool::Simple to create a few working threads. Each working thread does some stuff, including a call to chdir followed by an execution of an external Perl script (from the jbrowse genome browser, if it matters). I use capturex to call the external script and die on its failure.

I discovered that when I use more then one thread, things start to be messy. after some research. it seems that the current directory of some threads is not the correct one.

Perhaps chdir propagates between threads (i.e. isn't thread-safe)?
Or perhaps it's something with capturex?

So, how can I safely set the working directory for each thread?

** UPDATE **

Following the suggestions to change dir while executing, I'd like to ask how exactly should I pass these two commands to capturex?

currently I have:

my @args = ( "bin/flatfile-to-json.pl", "--gff=$gff_file", "--tracklabel=$track_label", "--key=$key", @optional_args );
capturex( [0], @args );

How do I add another command to @args?
Will capturex continue die on errors of any of the commands?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2024-10-01 01:14:43

我认为您可以通过放弃 IPC::System::Simple 来轻松解决“在运行命令之前如何在子进程中 chdir”问题,因为它不是完成这项工作的正确工具。

而不是做

my $output = capturex($cmd, @args);

类似的事情:

use autodie qw(open close);
my $pid = open my $fh, '-|';
unless ($pid) { # this is the child
  chdir($wherever);
  exec($cmd, @args) or exit 255;
}
my $output = do { local $/; <$fh> };
# If child exited with error or couldn't be run, the exception will
# be raised here (via autodie; feel free to replace it with 
# your own handling)
close ($fh);

如果您从 capturex 获取行列表而不是标量输出,唯一需要更改的是倒数第二行(到 my @output = <$fh>;)。

有关 forking-open 的更多信息,请参阅 perldoc perlipc

相对于 capture("chdirwherever; $cmd @args") 的好处是,它不会让 shell 有机会对您的 @args< 做坏事。 /代码>。

更新的代码(不捕获输出)

my $pid = fork;
die "Couldn't fork: $!" unless defined $pid;
unless ($pid) { # this is the child
  chdir($wherever);
  open STDOUT, ">/dev/null"; # optional: silence subprocess output
  open STDERR, ">/dev/null"; # even more optional
  exec($cmd, @args) or exit 255;
}
wait;
die "Child error $?" if $?;

I think that you can solve your "how do I chdir in the child before running the command" problem pretty easily by abandoning IPC::System::Simple as not the right tool for the job.

Instead of doing

my $output = capturex($cmd, @args);

do something like:

use autodie qw(open close);
my $pid = open my $fh, '-|';
unless ($pid) { # this is the child
  chdir($wherever);
  exec($cmd, @args) or exit 255;
}
my $output = do { local $/; <$fh> };
# If child exited with error or couldn't be run, the exception will
# be raised here (via autodie; feel free to replace it with 
# your own handling)
close ($fh);

If you were getting a list of lines instead of scalar output from capturex, the only thing that needs to change is the second-to-last line (to my @output = <$fh>;).

More info on forking-open is in perldoc perlipc.

The good thing about this in preference to capture("chdir wherever ; $cmd @args") is that it doesn't give the shell a chance to do bad things to your @args.

Updated code (doesn't capture output)

my $pid = fork;
die "Couldn't fork: $!" unless defined $pid;
unless ($pid) { # this is the child
  chdir($wherever);
  open STDOUT, ">/dev/null"; # optional: silence subprocess output
  open STDERR, ">/dev/null"; # even more optional
  exec($cmd, @args) or exit 255;
}
wait;
die "Child error $?" if $?;
夜无邪 2024-10-01 01:14:43

我不认为“当前工作目录”是每个线程的属性。我期望它是进程的一个属性。

不过,目前还不清楚为什么需要使用 chdir 。您能否不启动外部脚本来适当地设置进程的工作目录?这听起来是一个更可行的方法。

I don't think "current working directory" is a per-thread property. I'd expect it to be a property of the process.

It's not clear exactly why you need to use chdir at all though. Can you not launch the external script setting the new process's working directory appropriately instead? That sounds like a more feasible approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文