On Sun, 1 Jun 2008 07:32:39 -0700 (PDT), Leon zhang <leoncamel@gmai l.comwrote:
file.read() reads the entire contents of the file. Your code never closes
the write end of the pipe, so the read can never succeed - there is always
more for it to read.
Jean-Paul
>
>#!/usr/bin/env python
># -*- coding: utf-8 -*-
>
>import string, sys
>from threading import Thread
>import os
>import time
>
>class test_pipe(Threa d):
def __init__(self, fd):
Thread.__init__ (self)
self.testfd = fd
>
def run(self):
print "started thread begin -----"
while True:
buf = self.testfd.rea d()
print "receive %s" % (buf)
time.sleep(1)
#print "hoho"
>
>if __name__ == "__main__":
>
stdin_r, stdin_w = os.pipe()
#stdout_r, stdout_w = pipe()
>
f_w = os.fdopen(stdin _w, "w", 0)
>
thrd = test_pipe(os.fd open(stdin_r, "r", 0))
thrd.start()
>
time.sleep(1)
>
while True:
f_w.write("help \r\n")
time.sleep(1)
>
thrd.join()
>--------------------------------------------
>well, I want the following small test about pipe() in thread().
>OK, I write to the pipe in the main thread, and I created a new thread
>for reading from the pipe, then it will print what it received from
>the pipe().
>
>But, it seems it block at the "self.testfd.re ad()".
>
>So, is there and suggestion and explaination about it?
>#!/usr/bin/env python
># -*- coding: utf-8 -*-
>
>import string, sys
>from threading import Thread
>import os
>import time
>
>class test_pipe(Threa d):
def __init__(self, fd):
Thread.__init__ (self)
self.testfd = fd
>
def run(self):
print "started thread begin -----"
while True:
buf = self.testfd.rea d()
print "receive %s" % (buf)
time.sleep(1)
#print "hoho"
>
>if __name__ == "__main__":
>
stdin_r, stdin_w = os.pipe()
#stdout_r, stdout_w = pipe()
>
f_w = os.fdopen(stdin _w, "w", 0)
>
thrd = test_pipe(os.fd open(stdin_r, "r", 0))
thrd.start()
>
time.sleep(1)
>
while True:
f_w.write("help \r\n")
time.sleep(1)
>
thrd.join()
>--------------------------------------------
>well, I want the following small test about pipe() in thread().
>OK, I write to the pipe in the main thread, and I created a new thread
>for reading from the pipe, then it will print what it received from
>the pipe().
>
>But, it seems it block at the "self.testfd.re ad()".
>
>So, is there and suggestion and explaination about it?
the write end of the pipe, so the read can never succeed - there is always
more for it to read.
Jean-Paul