如何在 Perl 中以线程安全的方式更改当前目录?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可以通过放弃 IPC::System::Simple 来轻松解决“在运行命令之前如何在子进程中 chdir”问题,因为它不是完成这项工作的正确工具。
而不是做
类似的事情:
如果您从
capturex
获取行列表而不是标量输出,唯一需要更改的是倒数第二行(到my @output = <$fh>;
)。有关 forking-open 的更多信息,请参阅 perldoc perlipc。
相对于
capture("chdirwherever; $cmd @args")
的好处是,它不会让 shell 有机会对您的@args< 做坏事。 /代码>。
更新的代码(不捕获输出)
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
do something like:
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 (tomy @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)
我不认为“当前工作目录”是每个线程的属性。我期望它是进程的一个属性。
不过,目前还不清楚为什么需要使用 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.