[Tutor] Kill A Fuction Call?
Jeff Shannon
jeff@ccvcorp.com
Thu Jan 9 17:56:12 2003
Scott wrote:
>Is it possible to call a function, and kill it if it doesn't complete in
>a certain amount of time?
>
>
Sort of, but not easily, and the method of doing so depends on whether
you're writing the function yourself. If you *are*, then the simplest
way is to have the function code periodically check a timer and abort
itself if it's taken too long. (This can be combined with threads --
have the function's worker thread check a simple 'abort' flag, and have
a timer or other function set that flag as needed.)
If you do not have control over the function, then this is a very
difficult thing. Threads won't work, because Python won't let you kill
threads from outside, as you already know. You *can* run the function
in a separate process, using standard IPC mechanisms like pipes, because
processes (unlike threads) can be killed -- but processes are also
rather heavier and more awkward to use. Depending on just what it is
that you're doing, there may be some way around the limitations, too --
if you're waiting on a socket connection, say, there's a timeoutsocket
module available that will do this for you.
Jeff Shannon
Technician/Programmer
Credit International