I think you maybe just don’t see the result of what is happening. Here is a complete example that seems to work on my mailbox if I don't completely understand what you want. The main change I made was to set stdout for p in sys.stdout instead of subprocess.PIPE . Perhaps I do not understand the essence of your question, and this bit is crucial ...
Here's the full code and output:
In the process of sending (testing) (I called it test_comms.py). I'm on Windows now, so sorry .bat :
import time import subprocess import sys
myapp.bat is trivial:
echo "In the bat cave (script)" python myapp.py
myapp.py contains (using Queue , not Queue - the current Python 2 environment):
import Queue from Queue import Empty import threading import sys import time def get_input(): print("Started the listening thread") for line in iter(sys.stdin.readline, ''): print("line arrived to put on the queue\n") q.put(line) sys.stdin.close() print("Hi, I'm here via popen") q = Queue.Queue() threading.Thread(name = 'input-getter', target = get_input).start() print("stdin listener Thread created and started")
Output:
D:\>comms_test.py D:\>echo "In the bat cave (script)" "In the bat cave (script)" D:\>python myapp.py Hi, I'm here via popen Started the listening threadstdin listener Thread created and started line arrived to put on the queue line arrived to put on the queue ('Queue size is', 2) ('input:', 'my message\n') line arrived to put on the queue line arrived to put on the queue ('Queue size is', 3) ('input:', 'my message\n') line arrived to put on the queue line arrived to put on the queue ('Queue size is', 4) ('input:', 'my message\n') line arrived to put on the queue line arrived to put on the queue ('Queue size is', 5) ('input:', 'my message\n') line arrived to put on the queue line arrived to put on the queue D:\>('Queue size is', 6) ('input:', 'my message\n') ('Queue size is', 5) ('input:', 'my message\n') ('Queue size is', 4) ('input:', 'my message\n') ('Queue size is', 3) ('input:', 'my message\n') ('Queue size is', 2) ('input:', 'my message\n') ('Queue size is', 1) ('input:', 'my message\n') ('Queue size is', 0) no input ('Queue size is', 0) no input ('Queue size is', 0) no input