3

I have a python script which at one point is required to run a perl script, wait for it to finish, then continue.

As this case will only occur on a windows machine, I thought I could simply open a new cmd and run the perl script there, but I'm having difficulties doing so.

import os

os.system("start /wait cmd /c {timeout 10}")

should open a new cmd and sleep for 10 seconds, but it closes right away. I don't want to put the perl script in position of the timeout 10, as it is quite resource intensive. Another idea was to use a subprocess with call or Popen and wait.

perl_script = subprocess.call(['script.pl', params])

But I'm not sure what would happen to the stdout of the perl script in such a case.

I know the location and the parameters of the perl script.

How can I run a perl script from my python script, print the output (a lot) and wait for it to finish?

edit:

As suggested by @rchang, I added the subprocess with communicate as following and it works just as intended.

import subprocess, sys

perl = "C:\\perl\\bin\\perl.exe"
perl_script "C:\\scripts\\perl\\flamethrower.pl"
params = " --mount-doom-hot"

pl_script = subprocess.Popen([perl, perl_script, params], stdout=sys.stdout)
pl_script.communicate()

These are my first lines of perl, just a quick copy/past script to test this.

print "Hello Perld!\n";
sleep 10;
print "Bye Perld!\n";

2 Answers 2

4
import subprocess
import sys

perl_script = subprocess.Popen(["script.pl", params], stdout=sys.stdout)
perl_script.communicate()

This should hook up the stdout of the subprocess to the stdout stream of the Python script, provided you won't actually need the Python script to output anything else meaningful during execution that may not be related to the subprocess output.

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

2 Comments

The python script will wait for the perl script to finish with communicate(), correct? in this case the python script won't produce any output and this would be the desired behaviour, thank you very much.
@DaedalusMythos Yes, communicate() will wait for completion. The docs are a bit more verbose (link is to Python 2.7 documentation): docs.python.org/2/library/…
1

You could try:

perl_script = subprocess.check_output(["script.pl", params])
print perl_script

1 Comment

thank you for the suggestion, but this would give me the output all at once, when it's done, correct? I'll need the output as intended, because this informs me about the progress and possible errors.

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.