I am new to threading so I feel as if I am missing an obvious point, but I couldn't find an previous question that pertained to this subject.
I want to make a program that writes to stdin and reads stdout of a c program. This is the code in the main program.
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()
Thread(target=self.stream_watcher, name='stdout-listening', args=('STDOUT', self.proc.stdout)).start()
while True:
chat = raw_input('Enter chatter: ')
if len(chat) > 0:
chatter.put(chat)
if __name__ == '__main__':
chatt = Chatter()
chatt.main()
And here is the main.c program it invokes.
#include <stdio.h>
#include <stdlib.h>
int main(){
while (1){
int bytes_read;
size_t nbytes = 100;
char *my_string;
my_string = (char *)malloc(nbytes + 1);
bytes_read = getline (&my_string, &nbytes, stdin);
if (bytes_read == -1)
{
puts ("ERROR!");
}
else{
puts (my_string);
}
free(my_string);
}
return 0;
}
The current issue is that while it will run, stdout is never printed.