1

Using subprocess.call in python, I am trying to run a perl script, that runs a code on remote machines to fetch some information.

I have two scripts on machine A - python and perl. The python script calls the perl script using subprocess.call method by passing to it IP addresses in a loop. Both the scripts run just fine.

However, the perl script is executed in serial order (one IP after another).

# python code

def foo():
   print()
   IPs = ["198.168.1.2","198.168.3.4"]
   for ip in IPs:
       proc = call("perl script.pl '%s'" %ip, shell=True, stdin=PIPE)

foo()
   # perl script
   #!/usr/bin/perl

   sub bar($)
   {
      //Code that ssh's to a remote-machine and gets required information
   }

   print(bar($ARGV[0]),"\n");  
   print("Sleeping for 15 sec\n");
   sleep(15);
   print("Done\n");

Actual behavior -

  • The python script runs the perl script by passing '198.168.1.2'
  • perl script executes the code and sleeps for 15 sec
  • The python script then runs the perl script by passing ''198.168.3.4'
  • perl script executes the code and sleeps for 15 sec

Hence total of 30 seconds are required to run perl script for both the IP's.

Expected behavior -

  • The python script runs the perl script by passing '198.168.1.2'
  • Instead of waiting for perl script to finish with the first IP, it executes the same perl script with '198.168.3.4'.

Hence the time taken for executing the perl script for both the IP's is more or less the same.

1 Answer 1

1

This can be accomplished with subprocess.Popen. This runs the process without blocking the main thread. Your would would look something like this:

def foo():
    print()
    IPs = ["198.168.1.2","198.168.3.4"]
    procs = []
    for ip in IPs:
        proc = subproces.Popen(["perl", "script.pl", ip], shell=True, stdin=PIPE)
        procs.append(proc)

    # wait for all the processes to finish
    for proc in procs:
        proc.wait()

foo()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. I am not sure why, but proc = subproces.Popen(["perl", "script.pl", ip], shell=True, stdin=PIPE) didn't work. But, proc = Popen("perl script.pl '%s'" %ip, shell=True, stdin=PIPE) worked.
Huh. Okay. Would it be fair to ask you to mark my answer correct anyway? @Ira

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.