I want to write a python script which can launch an application.The application being launched can also read python commands which I am passing through another script. The problem I am facing is that I need to use two python scripts, one to launch an application and second one to run commands in launched application. Can I achieve this using a single script? How do I tell python to run next few lines of script in launched application?
1 Answer
In general, you use subprocess.Popen to launch a command from python. If you set it as non-blocking, it'll let you keep running python statements. You also have access to the running subprocesses stdin and stdout so you can interact with the running application.
If I understand what you're asking, it'd look something like this:
import subprocess
app = subprocess.Popen(["/path/to/app", "-and", "args"], stdin=subprocess.PIPE)
app.stdin.write("python command\n")
4 Comments
Tushar
Hello, Above answer can work. But I just want to check if there is any way to avoid passing commands as a string?
agoebel
How else would you propose to "pass" them? Talking between processes isn't simple as a rule. Without knowing specifically what app you are interacting with I'm not sure what to tell you. If the external app is also your own python code you can do funky things with generators and yields and do the whole thing in threads or something else. In general Inter Process Communication is a large topic.
agoebel
If you meant "How do I tell python to run next few lines of script in launched application?" meaning that the next few lines are executed in the launched app, there is no simple way to do that. You could use one giant multiline string instead which will still include the newline characters.
Tushar
I understand this can be very complicated.But option you is also workable. I think I can make peace with it for now. Thanks a lot for your quick response. I accept your answer.