I’m running a Perl script through the subprocess module in Python on Linux. The function that runs the script is called several times with variable input.
def script_runner(variable_input):
out_file = open('out_' + variable_input, 'wt')
error_file = open('error_' + variable_input, 'wt')
process = subprocess.Popen(['perl', 'script', 'options'], shell=False,
stdout=out_file, stderr=error_file)
However, if I run this function, say, twice, the execution of the first process will stop when the second process starts. I can get my desired behavior by adding
process.wait()
after calling the script, so I’m not really stuck. However, I want find out why I cannot run the script using subprocess as many times as I want, and have the script make these computations in parallel, without having to wait for it to finish between each run.
UPDATE
The culprit was not so exciting: the perl script used a common file that was rewritten for each execution.
However, the lesson I learned from this was that the garbage collector does not delete the process once it starts running, because this had no influence on my script once I got it sorted out.
If you are using Unix, and wish to run many processes in the background, you could use
subprocess.Popenthis way:x_fork_many.py:
The output looks something like this:
I’m not sure why you are getting the strange behavior when not calling
.wait(). However, the script above suggests (at least on unix) that savingsubprocess.Popen(...)processes in a list or set is not necessary. Whatever the problem is, I don’t think it has to do with garbage collection.PS. Maybe your perl scripts are conflicting in some way, which causes one to end with an error when another one is running. Have you tried starting multiple calls to the perl script from the command line?