realtime design

Aahz aahz at pythoncraft.com
Mon Oct 14 20:08:16 EDT 2002


In article <cb035744.0210140629.43767d40 at posting.google.com>,
Will Stuyvesant <hwlgw at hotmail.com> wrote:
>
>I am trying to come up with a method to run a function in such a
>way that the whole thing will take at most x milliseconds, with x a
>reasonable value like 200.  If the function is not finished by that
>time, a default value should be used.  If the function *does* finish in
>time, the return value of the function should be used.  The main point
>is that the calling program should be able to continue with the next
>statement after x milliseconds.  And it would be nice if the called
>function is interrupted or something if it has not finished, so it will
>not eat CPU resources.

That last part isn't really possible, not if there are statements that
depend on external connectivity like I/O.  But it's certainly possible
to make your main loop work the way you want: have your function put its
result on a Queue.Queue.  In your main function, time.sleep() for
however long it's supposed to, then do a non-blocking get() on the
queue.  If the get() fails, do whatever's appropriate.  This gets
trickier if your main program needs to be responsive during the 200ms,
but it's not impossible.

If your function is running in a loop, have it check the time at the top
or bottom of the loop and exit early if it takes too long.  That's not
perfect, but it should keep your program from consuming endless
resources.

>and-now-I-am-thinking-about-how-to-kill-a-Windows-process-ly y'rs

Not cleanly.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Project Vote Smart: http://www.vote-smart.org/



More information about the Python-list mailing list