Re: killing thread after timeout




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 .



Relevant Pages

  • Re: Establishing a p2p connection in python
    ... > programming PHP on a webdeveloper basis. ... > I have access to a public IP that has the ability to run python (but ... > users and establishing p2p connections on demand. ... It would seem like the socket ...
    (comp.lang.python)
  • Threading problem when many sockets open
    ... I have written a socket based service in python and under fairly heavy ... enqueues the connection on a Queue. ... connections do not pile up very quickly. ...
    (comp.lang.python)
  • Re: New to Python: my impression v. Perl/Ruby
    ... After years and years of Perl use, I evaluated Ruby ... > and Python and have adopted Python. ... the socket module docs for the default timeout setting. ... Yes, that's a substantive difference in design, I agree ...
    (comp.lang.python)
  • Re: Sockets on Windows and Mac
    ... > I am new to Python and have been writing some socket based programmes ... > on Windows, however I am unable to get them to work ... > on Mac. ...
    (comp.lang.python)
  • Re: socket.makefile() buggy?
    ... Python has been around for a long time, so you should ask yourself how ... import socket ... while reads < toread and retrytime < 10: ... but the client is actually sending 120. ...
    (comp.lang.python)