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?
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::Simpleas 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)