Readline()

Taylor, Stuart Stuart.Taylor at disney.com
Tue Mar 13 05:50:25 EDT 2007


i have a thread class which should read the output from the procedure
line by line and finish when the thread is set to kill:
 
class KillableThread(threading.Thread):
    def __init__(self, name="thread", *args, **kwargs):
        threading.Thread.__init__(self, *args, **kwargs)
        self.name = name
        self.killed = 0
    def kill(self):
        self.killed = 1
    def run(self):
        self.id = threading._get_ident()
        self.Proc      =
subprocess.Popen(["python","-tt","output_file.py"], cwd =
os.getcwd(),stdin=subprocess.PIPE, stdout = subprocess.PIPE, stderr =
subprocess.STDOUT)
        retval= None 
        while retval == None and not self.killed:
            line = self.Proc.stdout.readline()
            retval=self.Proc.poll()
 
to replicate my problem the output_file.py requires a user input. i ask
for a user input as my thread will sit waiting on
self.Proc.stdout.readline(). This is the point i want to have a timeout
or be able to end my thread as there is no output from the output_file. 
 
The main approch i cannot implement is to be able to kill the thread as
it remains hung on readlines()
 

________________________________

From: Sick Monkey [mailto:sickcodemonkey at gmail.com] 
Sent: 13 March 2007 01:51
To: Taylor, Stuart
Cc: python-list at python.org
Subject: Re: Readline()


Maybe if you show us your code we can better assist you.  

But maybe you can use a global variable or a try-catch method to keep
threads from staying alive and help better control your code.  


On 3/12/07, Taylor, Stuart <Stuart.Taylor at disney.com> wrote: 

	I have been working on running an external process using
subprocess.popen for a few days. 
	The process is ran over the network to another machine. 
	One thing I have found is that if I perform readline() on the
stdout it will hang if the process loses connection. 

	I have tried a few things in a test example: one is to use
stdin.write then stdin.flush() which send a string that readline() will
read and it ends correctly but doesn't work on the project I need it to
work on. Another is to try using threads and ignoar the thread when  the
process has lost connection but this still leaves the open thread alive
even when the main app has finished. 

	I am relatively new to python and I may be making a fundemantal
mistake in my implementation, can anyone please inform me of whether
sticking with subprocess.popen and readline() is the correct procedure
for this sort of task? 

	And if anyone can point me in the correct implementation of this
problem I would be gratefull. 


	Thank you 

	Stuart 


	--
	http://mail.python.org/mailman/listinfo/python-list
	


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070313/e71edf45/attachment.html>


More information about the Python-list mailing list