Asynchronously working reverse

Now I have a perl script that at some point collects and then processes the output of several bash commands, right now, here's how I did it:

if ($condition) { @output = `$bashcommand`; @output1 = `$bashcommand1`; @output2 = `$bashcommand2`; @output3 = `$bashcommand3`; } 

The problem is that each of these commands takes quite a lot of time, so I would like to know if I can run them all at the same time.

+4
source share
4 answers

This sounds like a good use case for Forks::Super::bg_qx .

 use Forks::Super 'bg_qx'; $output = bg_qx $bashcommand; $output1 = bg_qx $bashcommand1; $output2 = bg_qx $bashcommand2; $output3 = bg_qx $bashcommand3; 

will run these four commands in the background. Variables used for return values ​​( $output , $output1 , etc.) are overloaded objects. Your program will extract the output from these commands (waiting for the completion of the commands, if necessary) the next time these variables are specified in the program.

 ... more stuff happens ... # if $bashcommand is done, this next line will execute right away # otherwise, it will wait until $bashcommand finishes ... print "Output of first command was ", $output; &do_something_with_command_output( $output1 ); @output2 = split /\n/, $output2; ... 

Update 2012-03-01: v0.60 from Forks :: Super has several new constructs that allow you to retrieve results in a list context:

 if ($condition) { tie @output, 'Forks::Super::bg_qx', $bashcommand; tie @output1, 'Forks::Super::bg_qx', $bashcommand1; tie @output2, 'Forks::Super::bg_qx', $bashcommand2; tie @output3, 'Forks::Super::bg_qx', $bashcommand3; } ... 
+3
source

On a Unix system, you should be able to open several commands, then run a loop that calls IO::Select in order to wait for any of them to be ready to read; keep reading and laying out your output (with sysread ) until they reach the end of the file.

Unfortunately, apparently Win32 Unix select emulation cannot handle file I / O, so to pull it out on Windows, you also have to add an input / output socket layer for which select works, see perlmonks .

+4
source

You can, but not use backlinks.

Instead, you will need to open real files for them using open(handle, "$bashcommand|"); and then make the correct call to select to determine who has the new output that is ready for you. This will take a lot more than the 6 lines you specified above, but you can run them all at the same time.

There are several classes in CPAN that are likely to handle some of the complexity for you.

+2
source

You should consult the Perl FAQ .

Proc :: Background looks promising.

0
source

Source: https://habr.com/ru/post/1395079/


All Articles