我对线程是新手,所以我觉得我好像遗漏了一个明显的问题,但是我找不到与这个主题相关的前一个问题。在
我想做一个写stdin并读取c程序的stdout的程序。这是主程序中的代码。在from subprocess import Popen, PIPE
from threading import Thread
from Queue import Queue, Empty
from os import getcwd
import time
import random
chatter = Queue(maxsize=10) # Queue of strings to be sent to the program
class Chatter():
def stream_talker(self, identifier, stream):
while True:
if not chatter.empty():
self.proc.stdin.write(chatter.get(True, 1))
def stream_watcher(self, identifier, stream):
while True:
for line in stream:
print line
def main(self):
self.proc = Popen(getcwd() + '/main', stdout=PIPE, stdin=PIPE)
Thread(target=self.stream_talker, name='stdin-talker', args=('STDIN', self.proc.stdin)).start()
T