realtime design

Will Stuyvesant hwlgw at hotmail.com
Sun Oct 13 11:26:10 EDT 2002


<OP>
We would like to do prototyping of realtime systems.  Is
anybody doing something like this using Python?
</OP>

That posting has been reformulated to a programming contest:

James Besemer asked what we mean with realtime.  Realtime is
about responsiveness, for our purposes.  What we would like to
have is a function (or maybe a class, but a function seems
enough for now) that would make existing Python software
suitable for a realtime application.  The function should call
a function in the existing software.  The function should
return within a specified time limit.  That would mean a
function call could only take a maximum amount of time, after
that it is ignored or killed or whatever and a default value
is used.  So you can reuse existing software without modifying
anything in its source code.

I think this can be done.  I need a function, lets call it
rcall(...), that accepts a *maximumtime* parameter, a value
representing milliseconds, and that calls a function *f* in
the existing software.  If *f* returns before maximumtime
passes everything is just like when calling the *f* function
without rcall.  Else rcall returns 'timeout' at about time ==
*maximumtime*.  I can think of many other useful functions for
realtime software, but something like rcall(...) is the first
I would like to have.

For implementing this the signal module did look interesting.
But the signal module is not complete for Windows: e.g.
signal.alarm() and signal.pause() are UNIX only, so the signal
module seems not useful.  And it was mentioned that
time.sleep() does not work with signals on a Linux box.

Here is my first setup.  What I am asking is this: can anybody
write the realtime.rcall function body so it fits its
description?



<file realtime.py>

import time

def current_time():
    return time.time() * 1000   # ms since Januari 1 1970

def rcall(ms, func, *args, **kw):
    ''' Return func(*args, **kw), unless over ms milliseconds
    time passes.  In that case return 'timeout' in about ms
    milliseconds plus the time it takes for returning.

    NOT functioning now, just returning func(*args)
    '''
    starttime = current_time()

    # YOUR CODE HERE TO REPLACE THIS LINE :-)
    returnvalue = func(*args, **kw)   # How to set a timelimit here?

    endtime = current_time()
    if endtime - starttime > ms: 
        return 'timeout'
    else: 
        return returnvalue

</file realtime.py>



Testing it:

<file testrealtime.py>

import random, time
from realtime import rcall
from realtime import current_time

class TestMe:
    def waitForSeconds(self, seconds):
        time.sleep(seconds)
        pass
    def returnImmediately(self):
        return 1
    def bigCalculation(self): 
        # to make sure the cpu is busy
        x = [ [[]] * 5000 + 
            [random.random()] for i in range(1000)]
        x.sort()    # takes about 5 seconds on my computer
        return 1

starttime = current_time()
t = rcall(500, TestMe)
if t != 'timeout':
    print rcall(500, t.returnImmediately),
    print rcall(500, t.waitForSeconds, 2),
    print rcall(500, t.bigCalculation),
endtime = current_time()
print endtime - starttime

</file testrealtime.py>



-- DESIRED output: 
1 timeout timeout 1050.0
-- or another value around 1050.0, only t.waitForSeconds and
-- t.bigCalculation should take a little over 500 ms, the
-- others should be very fast

-- The output now is:
1 timeout timeout 5938.0
-- of course, since there the time limit in realtimecall.rcall
-- is not implemented.



QOTD
'''
===  ALL CSH USERS PLEASE NOTE  ========================

Set the variable $LOSERS to all the people that you think are losers.
This will cause all said losers to have the variable
$PEOPLE-WHO-THINK-I-AM-A-LOSER updated in their .login file.  Should you
attempt to execute a job on a machine with poor response time and a
machine on your local net is currently populated by losers, that machine
will be freed up for your job through a cold boot process.
'''



More information about the Python-list mailing list