Home > Mobile >  Python - call perl as subprocess - wait for finish its background process and print to shell
Python - call perl as subprocess - wait for finish its background process and print to shell

Time:10-15

I have a Perl script, that inside calls system("something &"). If I run it from shell, I call the script and later use wait command to wait for all background processes.

How can I call this script from Python and wait for the "background" scripts it spawned to finish?

I have tried

pipe = subprocess.Popen(["perl", "script.pl", "data1", "data2"], stdin=subprocess.PIPE)
pipe.communicate()

but it wont wait and quits after the perl script finishes.

Another problem is I dont know how to print the output of background processes to shell while it is running.

CodePudding user response:

I have fix this by changing the Perl script based on this answer How can I make Perl wait for child processes started in the background with system()?

I have changed

system("something &") 

to

my $pid = fork();
if ($pid == -1) {
   print("Failed to fork");
   die;
} elsif ($pid == 0) {
   exec "something ";
}

and add

while (wait() != -1) {}

to the script end

  • Related