From: Slobodan Blazeski
Subject: How to make time limited function?
Date: 
Message-ID: <31006ec4-96f8-4209-ae15-1dc7feba9099@e25g2000prg.googlegroups.com>
How to make a time aware function that will return the result or
whatever if it goes over it's time limit?
(calculate-last-years-expenditures datasheet :secunds 15)
120 000 000
or
not-finished-yet if it goes over it's time slice

cheers
Slobodan

From: Brian
Subject: Re: How to make time limited function?
Date: 
Message-ID: <a7b0b8ef-1c37-4acd-a5ad-4236533daf01@k13g2000hse.googlegroups.com>
Slobodan Blazeski wrote:
> How to make a time aware function that will return the result or
> whatever if it goes over it's time limit?
> (calculate-last-years-expenditures datasheet :secunds 15)
> 120 000 000
> or
> not-finished-yet if it goes over it's time slice
>
> cheers
> Slobodan
Less than ideal solution: At the start of the function you grab the
current time with GET-UNIVERSAL-TIME, then you have the function
periodically check GET-UNIVERSAL-TIME to see how much time has elapsed.
From: Slobodan Blazeski
Subject: Re: How to make time limited function?
Date: 
Message-ID: <fd7b73c7-5c7a-457b-bfb4-2c1eaf387b05@s19g2000prg.googlegroups.com>
On Mar 12, 10:06 pm, Brian <··············@gmail.com> wrote:
> Slobodan Blazeski wrote:
> > How to make a time aware function that will return the result or
> > whatever if it goes over it's time limit?
> > (calculate-last-years-expenditures datasheet :secunds 15)
> > 120 000 000
> > or
> > not-finished-yet if it goes over it's time slice
>
> > cheers
> > Slobodan
>
> Less than ideal solution: At the start of the function you grab the
> current time with GET-UNIVERSAL-TIME, then you have the function
> periodically check GET-UNIVERSAL-TIME to see how much time has elapsed.

And where does that periodic checks should be placed exactly?
What about the function that calls another function?
And I don't even want to think about how expensive is get-u-time.

Slobodan
http://tourdelisp.blogspot.com/
From: Edi Weitz
Subject: Re: How to make time limited function?
Date: 
Message-ID: <ubq5j91lt.fsf@agharta.de>
On Wed, 12 Mar 2008 12:26:35 -0700 (PDT), Slobodan Blazeski <·················@gmail.com> wrote:

> How to make a time aware function that will return the result or
> whatever if it goes over it's time limit?
> (calculate-last-years-expenditures datasheet :secunds 15)
> 120 000 000
> or
> not-finished-yet if it goes over it's time slice

That's implementation-dependent.  See for example here:

  http://www.lispworks.com/documentation/lw50/LWUG/html/lwuser-188.htm

Edi.

-- 

European Common Lisp Meeting, Amsterdam, April 19/20, 2008

  http://weitz.de/eclm2008/

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Slobodan Blazeski
Subject: Re: How to make time limited function?
Date: 
Message-ID: <1422f94c-5fc3-41b6-99ca-85a31d8f33e1@h11g2000prf.googlegroups.com>
On Mar 12, 8:35 pm, Edi Weitz <········@agharta.de> wrote:
> On Wed, 12 Mar 2008 12:26:35 -0700 (PDT), Slobodan Blazeski <·················@gmail.com> wrote:
> > How to make a time aware function that will return the result or
> > whatever if it goes over it's time limit?
> > (calculate-last-years-expenditures datasheet :secunds 15)
> > 120 000 000
> > or
> > not-finished-yet if it goes over it's time slice
>
> That's implementation-dependent.  See for example here:
>
>  http://www.lispworks.com/documentation/lw50/LWUG/html/lwuser-188.htm
>
> Edi.


Thanks Edi. Do you have some experience with it, like running a huge
amount of them?

Slobodan
From: George Neuner
Subject: Re: How to make time limited function?
Date: 
Message-ID: <5uigt3l969iqs4shd4m4ie6rfp9kbu56n5@4ax.com>
On Wed, 12 Mar 2008 12:40:23 -0700 (PDT), Slobodan Blazeski
<·················@gmail.com> wrote:

>On Mar 12, 8:35�pm, Edi Weitz <········@agharta.de> wrote:
>> On Wed, 12 Mar 2008 12:26:35 -0700 (PDT), Slobodan Blazeski <·················@gmail.com> wrote:
>> > How to make a time aware function that will return the result or
>> > whatever if it goes over it's time limit?
>> > (calculate-last-years-expenditures datasheet :secunds 15)
>> > 120 000 000
>> > or
>> > not-finished-yet if it goes over it's time slice
>>
>> That's implementation-dependent. �See for example here:
>>
>> �http://www.lispworks.com/documentation/lw50/LWUG/html/lwuser-188.htm
>>
>> Edi.
>
>
>Thanks Edi. Do you have some experience with it, like running a huge
>amount of them?
>
>Slobodan

What I believe you're asking about is called an "engine" in Scheme.
An explanation and simple examples can be found here:
http://gd.tuwien.ac.at/languages/scheme/tutorial-dsitaram/t-y-scheme-Z-H-16.html

However, in Scheme, the simple implementation relies on general
continuations.  Offhand, I can think of a few complicated ways to
express the functionality in CL ... but not a simple way.

George
--
for email reply remove "/" from address
From: Slobodan Blazeski
Subject: Re: How to make time limited function?
Date: 
Message-ID: <8e3c3160-c39d-40c6-a199-3d3019d9765a@e10g2000prf.googlegroups.com>
On Mar 12, 10:44 pm, George Neuner <·········@/comcast.net> wrote:
> On Wed, 12 Mar 2008 12:40:23 -0700 (PDT), Slobodan Blazeski
>
>
>
>
>
> <·················@gmail.com> wrote:
> >On Mar 12, 8:35 pm, Edi Weitz <········@agharta.de> wrote:
> >> On Wed, 12 Mar 2008 12:26:35 -0700 (PDT), Slobodan Blazeski <·················@gmail.com> wrote:
> >> > How to make a time aware function that will return the result or
> >> > whatever if it goes over it's time limit?
> >> > (calculate-last-years-expenditures datasheet :secunds 15)
> >> > 120 000 000
> >> > or
> >> > not-finished-yet if it goes over it's time slice
>
> >> That's implementation-dependent.  See for example here:
>
> >>  http://www.lispworks.com/documentation/lw50/LWUG/html/lwuser-188.htm
>
> >> Edi.
>
> >Thanks Edi. Do you have some experience with it, like running a huge
> >amount of them?
>
> >Slobodan
>
> What I believe you're asking about is called an "engine" in Scheme.
> An explanation and simple examples can be found here:http://gd.tuwien.ac.at/languages/scheme/tutorial-dsitaram/t-y-scheme-...
>
> However, in Scheme, the simple implementation relies on general
> continuations.
What about delimited ones?
> Offhand, I can think of a few complicated ways to
> express the functionality in CL ... but not a simple way.
Give me whatever you have on your, starting from scratch is the
hardest.
From: George Neuner
Subject: Re: How to make time limited function?
Date: 
Message-ID: <cfrjt31tv16itgtj1ul4p927lka0healev@4ax.com>
On Thu, 13 Mar 2008 02:13:46 -0700 (PDT), Slobodan Blazeski
<·················@gmail.com> wrote:

>On Mar 12, 10:44�pm, George Neuner <·········@/comcast.net> wrote:
>> On Wed, 12 Mar 2008 12:40:23 -0700 (PDT), Slobodan Blazeski
>>
>>
>>
>>
>>
>> <·················@gmail.com> wrote:
>> >On Mar 12, 8:35�pm, Edi Weitz <········@agharta.de> wrote:
>> >> On Wed, 12 Mar 2008 12:26:35 -0700 (PDT), Slobodan Blazeski <·················@gmail.com> wrote:
>> >> > How to make a time aware function that will return the result or
>> >> > whatever if it goes over it's time limit?
>> >> > (calculate-last-years-expenditures datasheet :secunds 15)
>> >> > 120 000 000
>> >> > or
>> >> > not-finished-yet if it goes over it's time slice
>>
>> >> That's implementation-dependent. �See for example here:
>>
>> >> �http://www.lispworks.com/documentation/lw50/LWUG/html/lwuser-188.htm
>>
>> >> Edi.
>>
>> >Thanks Edi. Do you have some experience with it, like running a huge
>> >amount of them?
>>
>> >Slobodan
>>
>> What I believe you're asking about is called an "engine" in Scheme.
>> An explanation and simple examples can be found here:http://gd.tuwien.ac.at/languages/scheme/tutorial-dsitaram/t-y-scheme-...
>>
>> However, in Scheme, the simple implementation relies on general
>> continuations.
>What about delimited ones?

I have no experience with delimited continuations, but based on what I
have read I suspect they could probably be made to work.  

>>�Offhand, I can think of a few complicated ways to
>> express the functionality in CL ... but not a simple way.
>Give me whatever you have on your, starting from scratch is the
>hardest.

It would probably be easiest to use whatever threads your Lisp
provides (or a portable thread package) and yield on a timer
condition.


Multi-tasking without continuations of any kind is hard ... the
simplest implementation would be a state machine with transacted
state.  I don't have any ready code and it would take a while to hack
up a demo that works, but I can describe the general idea:

Break the computation into a series of small steps - functions that
can always finish within one time quantum.  Define a structure or
object to maintain the current step/function and its starting input.
As each step completes, you (atomically) update the structure to
reflect the next step and the new partial results (aka "input to the
next step").  If your timer hasn't expired yet you can go on to start
the next step.

[If you don't have timers you can calculate the abort time on entry
and have each step function check it against the current time.  Or you
can just return from the entry point after each step finishes.]

The entry point to the SM should set a timer and call the function
representing the current step providing the contents of the state
structure as input.  When the timer expires, you can just return from
the entry point because the current step will be restarted from the
beginning the next time the entry point is called.  

If you have several such machines, you can call them round-robin from
a driver loop until you're satisfied with the results.  As long as
your timer quantum is sufficient to complete at least one step at each
round, the computation will always advance.

I used to do embedded programming in C and I've had to do this kind of
thing to multi-task in situations where I had no OS, no ready made
thread library, and (for whatever reason) was not permitted to
engineer ad-hoc threading.

George
--
for email reply remove "/" from address
From: Edi Weitz
Subject: Re: How to make time limited function?
Date: 
Message-ID: <u7ig790z7.fsf@agharta.de>
On Wed, 12 Mar 2008 12:40:23 -0700 (PDT), Slobodan Blazeski <·················@gmail.com> wrote:

> Do you have some experience with it, like running a huge amount of
> them?

Nope.  What do you need that for?

-- 

European Common Lisp Meeting, Amsterdam, April 19/20, 2008

  http://weitz.de/eclm2008/

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Slobodan Blazeski
Subject: Re: How to make time limited function?
Date: 
Message-ID: <4c3b0791-e768-487a-b650-2c110e84445a@e23g2000prf.googlegroups.com>
On Mar 12, 8:49 pm, Edi Weitz <········@agharta.de> wrote:
> On Wed, 12 Mar 2008 12:40:23 -0700 (PDT), Slobodan Blazeski <·················@gmail.com> wrote:
> > Do you have some experience with it, like running a huge amount of
> > them?
>
> Nope.  What do you need that for?
Poor man actor model.
From: Lars Rune Nøstdal
Subject: Re: How to make time limited function?
Date: 
Message-ID: <47d8447a$0$28885$c83e3ef6@nn1-read.tele2.net>
On Wed, 12 Mar 2008 12:26:35 -0700, Slobodan Blazeski wrote:

> How to make a time aware function that will return the result or
> whatever if it goes over it's time limit?
> (calculate-last-years-expenditures datasheet :secunds 15) 120 000 000
> or
> not-finished-yet if it goes over it's time slice
> 
> cheers
> Slobodan

SBCL has SB-EXT:WITH-TIMEOUT


(defmacro my-with-timeout ((seconds &body on-timeout) &body body)
  (let ((the-condition (gensym)))
    `(restart-case
         (handler-bind
             ((sb-ext:timeout (lambda (,the-condition)
                                (declare (ignore ,the-condition))
                                ,@on-timeout)))
           (sb-ext:with-timeout ,seconds
             ,@body))
       (skip-body ()
         :report "Skip the BODY-form of WITH-TIMEOUT."))))

;; (export 'skip-body) might make sense doing this


(defun test-timeout (n)
  (my-with-timeout ((1- n)
                    (format t "timeout after ~A seconds~%" (1- n))
                    (format t "continue (0), skip-body (1) or do nothing here (2): ")
                    (case (read)
                      (0 (invoke-restart 'continue))
                      (1 (invoke-restart 'skip-body))
                      (t )))
    (dotimes (i n)
      (format t "..waiting (~A)~%" i)
      (sleep 1))
    (write-line "will there be a time for me?"))
  (write-line "the end"))


CL-USER> (test-timeout 5)
..waiting (0)
..waiting (1)
..waiting (2)
..waiting (3)
..waiting (4)
timeout after 4 seconds
continue (0), skip-body (1) or do nothing (2): 0
will there be a time for me?
the end
"the end"
CL-USER> (test-timeout 5)
..waiting (0)
..waiting (1)
..waiting (2)
..waiting (3)
timeout after 4 seconds
continue (0), skip-body (1) or do nothing (2): 1
the end
"the end"
CL-USER> (test-timeout 5)
..waiting (0)
..waiting (1)
..waiting (2)
..waiting (3)
timeout after 4 seconds
continue (0), skip-body (1) or do nothing (2): 2
; Evaluation aborted.
CL-USER> 


.. not sure if this is what you're after or if it 100% correct ..

-- 
Lars Rune Nøstdal
http://nostdal.org/
From: ·······@eurogaran.com
Subject: Re: How to make time limited function?
Date: 
Message-ID: <b92f9ae1-4e31-4b4f-bc3a-04fa7bc35c2d@t54g2000hsg.googlegroups.com>
I have made a macro wrapper which does precisely that.
Find it at :
http://www.eurogaran.com/downloads/lisp/limitools/limitime.lsp

Hope it helps.
From: Slobodan Blazeski
Subject: Re: How to make time limited function?
Date: 
Message-ID: <f1fd28da-bc08-4afb-b3a3-fa1ac0412abd@d4g2000prg.googlegroups.com>
On Mar 13, 12:45 pm, ·······@eurogaran.com wrote:
> I have made a macro wrapper which does precisely that.
> Find it at :http://www.eurogaran.com/downloads/lisp/limitools/limitime.lsp
>
> Hope it helps.

Many thanks.

Slobodan
From: Espen Vestre
Subject: Re: How to make time limited function?
Date: 
Message-ID: <m1k5k7dovu.fsf@gazonk.vestre.net>
Slobodan Blazeski <·················@gmail.com> writes:

> How to make a time aware function that will return the result or
> whatever if it goes over it's time limit?
> (calculate-last-years-expenditures datasheet :secunds 15)
> 120 000 000
> or
> not-finished-yet if it goes over it's time slice

In any lisp with mp support you can solve this by running a separate
"watcher" thread that kills the function-running thread when it
exceeds its time limit. It's not a good idea to use this brute force
method in general, though, since you may happen to kill the thread
when it's running the cleanup part of an unwind-protect. You need
consider your own timeout needs and see if they can be implemented in
a less brute force manner, e.g. socket timeouts could be handled
during socket i/o-operations and not through a general timeout
operation.
-- 
  (espen)