killing thread after timeout

Bryan Olson fakeaddress at nowhere.org
Tue Sep 6 16:03:01 EDT 2005


Jacek Poplawski had written:
 >> 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.

Jacek Poplawski wrote:
 > After reading more archive I think that solution may be to raise an
 > Exception after timeout, but how to do it portable?

Python allows any thread to raise a KeyboardInterrupt in the
main thread (see thread.interrupt_main), but I don't think there
is any standard facility to raise an exception in any other
thread. I also believe, and hope, there is no support for lower-
level killing of threads; doing so is almost always a bad idea.
At arbitrary kill-times, threads may have important business
left to do, such as releasing locks, closing files, and other
kinds of clean-up.

Processes look like a better choice than threads here. Any
decent operating system will put a deceased process's affairs
in order.


Anticipating the next issues: we need to spawn and connect to
the various worker processes, and we need to time-out those
processes.

First, a portable worker-process timeout: In the child process,
create a worker daemon thread, and let the main thread wait
until either the worker signals that it is done, or the timeout
duration expires. As the Python Library Reference states in
section 7.5.6:

     A thread can be flagged as a "daemon thread". The
     significance of this flag is that the entire Python program
     exits when only daemon threads are left.

The following code outlines the technique:

     import threading

     work_is_done = threading.Event()

     def work_to_do(*args):
         # ... Do the work.
         work_is_done.set()

     if __name__ == '__main__':
         # ... Set stuff up.
         worker_thread = threading.Thread(
                 target = work_to_do,
                 args = whatever_params)
         worker_thread.setDaemon(True)
         worker_thread.start()
         work_is_done.wait(timeout_duration)



Next, how do we connect the clients to the worker processes?

If Unix-only is acceptable, we can set up the accepting socket,
and then fork(). The child processes can accept() incomming
connections on its copy of the socket. Be aware that select() on
the process-shared socket is tricky, in that that the socket can
select as readable, but the accept() can block because some
other processes took the connection.


If we need to run on Windows (and Unix), we can have one main
process handle the socket connections, and pipe the data to and
from worker processes. See the popen2 module in the Python
Standard Library.


-- 
--Bryan



More information about the Python-list mailing list