killing thread after timeout

Steve Horsley steve.horsley at gmail.com
Tue Sep 6 20:06:15 CEST 2005


Jacek Popławski wrote:
> Hello.
> 
> I am going to write python script which will read python command from 
> socket, run it and return some values back to socket.
> 
> My problem is, that I need some timeout. I need to say for example:
> 
> os.system("someapplication.exe")
> 
> and kill it, if it waits longer than let's say 100 seconds
> 
> I want to call command on separate thread, then after given timeout - 
> kill thread, but I realized (after reading Usenet archive) that there is 
> no way to kill a thread in Python.
> 
> How can I implement my script then?
> 
> PS. it should be portable - Linux, Windows, QNX, etc


Probably the easiest way is to use select with a timeout (see the 
docs for library module select). eg.

     a, b c = select.select([mySocket], [], [], timeout)
     if len(a) == 0:
         print 'We timed out'
     else:
         print 'the socket has something for us'


Steve



More information about the Python-list mailing list