Reputation: 2537
I am trying to utilize a python subprocess to run an executable binary with input.
My executable takes as input a JSON object and does stuff with it. If you run the executable on its own it will block waiting for input from stdin.
The following command runs just fine on the terminal:
echo '{"values":{"value_1":"1122","value_2":"abcd"}}' | ../path/to/bin/executable-script -option1=x -topic my_kafka_topic
But I need to generate the input JSONs in my python script. so I am right now trying this:
import sys
import subprocess
if __name__ == '__main__':
input_string = '{\"values\":{\"value_1\":\"1122\",\"value_2\":\"abcd\"}}'
input_bytes = [elem.encode("hex") for elem in input_string]
bashCommand = '../path/to/bin/executable-script -option1=x -topic my_kafka_topic'
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
print 'program blocks on next line'
data, error = process.communicate(input=input_bytes)
print 'data is {}'.format(data)
unfortunately my program will not run inside the python script, it will just block waiting for me to type in input. if I type in input it works fine.
but I don't want it to block. I want it to run just like in the command line. how do I do this?
Upvotes: 0
Views: 104
Reputation: 295353
Passing an argument to process.communicate()
is correct, but you also need to pass stdin=subprocess.PIPE
. Thus:
command = ['../path/to/bin/executable-script', '-option1=x', '-topic', 'my_kafka_topic']
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# ^^^^^^^^^^^^^^^^^^^^^
process.communicate(input_data)
Upvotes: 1