How to write a python script that can communicate with the input and output of a fast executable file?

So, I have a simple fast program, one file, a main.swiftprogram that looks like this.

import Foundation

var past = [String]()

while true {
    let input = readLine()!
    if input == "close" {
        break
    }
    else {
        past.append(input)
        print(past)
    }
}

I want to write a python script that can send an input string to this program, and then return the result of this and run it over time. I cannot use command line arguments since I need to maintain a fast executable file over time.

I tried os.system()and subprocess.call(), but it always gets stuck, because none of them gives input for a quick program, but they run the executable file. My shell is stuck mostly awaiting my input without receiving input from my python program.

This is my last python script, I tried:

import subprocess

subprocess.call("./Recommender", shell=True)
f = subprocess.call("foo", shell=True)
subprocess.call("close", shell=True)

print(f)

, ?

EDIT:

,

import subprocess
print(True)
channel = subprocess.Popen("./Recommender", shell = False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(True)
channel.stdin.write(("foo").encode())
channel.stdin.flush()
print(True)
f = channel.stdout.readline()

channel.terminate()
print(f)
print(True)

, stdout - , ?

+4
1

, - , . , .

process = subprocess.Popen("./Recommender", shell=True, stdin=subprocess.PIPE)
process.stdin.write(('close').encode())
process.stdin.flush()
+2

All Articles