Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python join subprocesses

I am executing shell commands using python. It works fine, but when I try to execute multiple commands, it executes in different process. eg:

1. set NAME=XYZ
2. calabash-android run myApp.apk

I am executing these 2 commands, using the following code (commands are assigned to variable bash_cmd):

f_handle = open('test_output.txt','w+')
process = subprocess.Popen(bash_cmd, shell=True, stdout=sys.stdout)
process.wait()            
f_handle.close()

Since a different process is created every time, I cannot access the NAME set in previous command. I could write a batch file, but again the value of variable NAME changes dynamically. I want both the commands to execute in the same process.

Is there a way to batch these commands in python, or maybe join the subprocesses. Please help!!

like image 581
Mayur More Avatar asked Sep 21 '26 18:09

Mayur More


1 Answers

Each subprocess.Popen creates a new process. If you want to execute several commands within the same shell then you could pass them all at once:

from subprocess import check_call

check_call("\n".join(shell_commands), shell=True)

You could also start a shell process and feed it commands one by one via its stdin:

from subprocess import Popen, PIPE

shell = Popen("/bin/sh", stdin=PIPE, bufsize=1)
for shell_command in iter(commands_queue.get, None):
    print >>shell.stdin, shell_command # write command
shell.stdin.close()
if shell.wait() != 0:
    raise RuntimeError(shell.returncode)

In your particular case, you could pass the modified environment to the subprocess directly:

import os
from subprocess import check_call

env = os.environ.copy()
env['NAME'] = 'XYZ'
check_call(["calabash-android", "run", "myApp.apk"], env=env)
like image 197
jfs Avatar answered Sep 23 '26 10:09

jfs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!