Medita Inc.
Medita Inc.

Reputation: 57

Keep the subprocess program running and accepting new arguments

I have the program that takes about 4-5 seconds to launch each time I open it with Python's subprocess:

command = ['./command', arg1] # my command to launch the program
p = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=-1) # launch the program (takes about 4 seconds) 
print(p.stdout.read()) # display output

I would like to avoid waiting for the program to launch each time and keep it running instead and pass the new args. Is there a way to keep the command running and passing in new arguments to output?

I was hoping something like this:

  p = launch_the_program()
  p.run(arg1)
  p.run(arg2)
  p.run(arg3)
  p.close()

I understand that this question is similar to mine, but the answer from there I think is outdated since it didn't work for me.

EDIT: the linked answer is about passing input to the program instead of arguments (which is what I'm trying to achieve). Is there a workaround that can keep program running and accepting new arguments without restarting?

Upvotes: 0

Views: 176

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 296049

When run returns, the program you ran has exited and is no longer in memory. There's no pre-initialized copy still in RAM to reuse (by re-invoking its main with different arguments).

Upvotes: 1

Related Questions