Bidrectional Subprocess Communication

Emanuele D'Arrigo manu3d at gmail.com
Sat Dec 13 15:19:29 EST 2008


Hi everybody,

I'm trying to replicate the positive results of the Client/Server
scripts from the thread "Bidirectional Networking", but this time
using a Process/SubProcess architecture.

The SubProcess, acting as a server, is working just fine. But the
Process executing the SubProcess, the client, somehow doesn't hear any
of the standard output from the SubProcess. What am I missing?

Below you can find the two short pieces of code. Thanks for your help!

Manu

-------------------------------------------
# serverTest.py
import sys

print("Starting Server!")

while True:
    data = sys.stdin.readline().strip()
    if(data):
        print("SERVER RECV: '" + data + "'")

    if(data == "Stop!"):
        break

print("Server Stopped!")

-------------------------------------------
#clientTest.py

import sys
import threading
from time import sleep
from subprocess import Popen, PIPE

## Server Thread
class ListenerThread(threading.Thread):

    def __init__(self, inChannel):
        threading.Thread.__init__(self)
        self.inChannel = inChannel

    def run(self):
        while True:
            data = self.inChannel.readline().strip()
            print("serverTest.py says: " + data)

print("Starting Client!")

server = Popen("python serverTest.py", stdin=PIPE, stdout=PIPE)

listenerThread = ListenerThread(server.stdout)
listenerThread.setDaemon(True)
listenerThread.start()

server.stdin.write("Something very meaningful!\n")
server.stdin.write("Stop!")

print("Client Stopped!")



More information about the Python-list mailing list