Z.Zt. experimentiere ich mit dem Modul subprocess. Ich möchte eine Kommunikation mit dem aufgerufenen Programm über stdin/stdout machen (so wie bei gnuChess). Aber leider scheint da noch ein Logikfehler zu sein, denn ich bekomme manchmal die Rückgabe verzögert. Ich poste mal die Scripte und die Ausgabe. Vielleicht kann mir ja jemand helfen?!
Code: Alles auswählen
#! /usr/bin/python
# main.py
from subprocess import Popen, PIPE
import time
import sys
p = Popen('python sub.py', shell=True, stdin=PIPE, stdout=PIPE)
while True:
s_in = raw_input('> ')
print '>>> '+s_in
p.stdin.write(s_in+'\n')
p.stdin.flush()
s_out = ''
while not s_out:
s_out = p.stdout.readline()
sys.stdout.write(s_out)
Code: Alles auswählen
#! /usr/bin/python
# sub.py
import sys
while True:
s_in = sys.stdin.readline()
if s_in != '':
s_out = 'Sub: ' + s_in + '\n'
sys.stdout.write(s_out)
sys.stdout.flush()
Code: Alles auswählen
> fdsfda
>>> fdsfda
Sub: fdsfda
> fsdfdfdsfsd
>>> fsdfdfdsfsd
> 34
>>> 34
Sub: fsdfdfdsfsd
>