From: Nonzero
Subject: Breaking Loops or Evals?
Date: 
Message-ID: <376b0355.0303091412.51e7bb80@posting.google.com>
Hello,

I don't know if this is possible, but I thought I would ask.

Is it possible to stop a loop or an eval statement after a given time
or number of iterations has passed even if the loop or eval statement
has not finished?  For example,  I would like a program that can run a
loop, but can terminate the loop if it has not finished on its own
after a given amount of time.

Thank you for your time.
-Colin

From: Nils Goesche
Subject: Re: Breaking Loops or Evals?
Date: 
Message-ID: <87llzogovp.fsf@darkstar.cartan>
····@geneseo.edu (Nonzero) writes:

> Is it possible to stop a loop or an eval statement after a
> given time or number of iterations has passed even if the loop
> or eval statement has not finished?  For example, I would like
> a program that can run a loop, but can terminate the loop if it
> has not finished on its own after a given amount of time.

One way is to check GET-INTERNAL-REAL-TIME now and then, another
to use threads (or ``processes��) and something like
MP:PROCESS-KILL.

Regards,
-- 
Nils G�sche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0
From: Nonzero
Subject: Re: Breaking Loops or Evals?
Date: 
Message-ID: <376b0355.0303100646.6b2a7b07@posting.google.com>
I have received a few suggestions on how to do this using an MP
statement like this:


(mp:with-timeout (10 (print "Timed out"))
	your-code-here) 


This is exactly what I need to do,  but it does not seem to work in
common lisp.  I get the error:


*** - READ from #<INPUT BUFFERED FILE-STREAM CHARACTER #P"test.lisp"
@1>: there is no package with name "MP"


Any ideas or suggestions?

Thank you for your time.

-Colin
From: Tim Bradshaw
Subject: Re: Breaking Loops or Evals?
Date: 
Message-ID: <ey3fzpvff4z.fsf@cley.com>
* Nonzero  wrote:

> This is exactly what I need to do,  but it does not seem to work in
> common lisp.  I get the error:

There is no way of doing it in CL (unless you modify your code to
check the time periodically).  You need some kind of `monitor' which
watches the process doing the calculation and stops it when it has had
too long.

--tim
From: Nils Goesche
Subject: Re: Breaking Loops or Evals?
Date: 
Message-ID: <lyvfyrdvvz.fsf@cartan.de>
Tim Bradshaw <···@cley.com> writes:

> * Nonzero  wrote:
> 
> > This is exactly what I need to do,  but it does not seem to work in
> > common lisp.  I get the error:
> 
> There is no way of doing it in CL (unless you modify your code to
> check the time periodically).  You need some kind of `monitor' which
> watches the process doing the calculation and stops it when it has
> had too long.

Here is a version for LispWorks:

(defmacro with-timeout ((timeout &optional result) &body body)
  (let ((proc (gensym "PROC"))
        (blockname (gensym "BLOCKNAME")))
    `(block ,blockname
       (let ((,proc (mp:process-run-function
                     (format nil "Timeout Function for ~A"
                             (mp:process-name mp:*current-process*))
                     (list :priority (mp:process-priority
                                      mp:*current-process*))
                     (lambda (,proc)
                       (sleep ,timeout)
                       (mp:process-interrupt ,proc
                                             (lambda ()
                                               (return-from ,blockname
                                                            ,result))))
                     mp:*current-process*)))
         (unwind-protect
             (progn
               ,@body)
           (mp:process-kill ,proc))))))

I didn't test much but it seems to work fine...

Regards,
-- 
Nils G�sche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Nonzero
Subject: Re: Breaking Loops or Evals?
Date: 
Message-ID: <376b0355.0303101723.262cf2e5@posting.google.com>
Thanks for the help.

I tried your code, but got the familiar 

*** - READ from #<INPUT BUFFERED FILE-STREAM CHARACTER #P"test.lisp" @7>: there
is no package with name "MP"

Error.

I'm using CLISP.  I don't know if that can help anyone help me :)


Thanks again everyone.
-Colin


Nils Goesche <······@cartan.de> wrote in message news:<··············@cartan.de>...
> Tim Bradshaw <···@cley.com> writes:
> 
> > * Nonzero  wrote:
> > 
> > > This is exactly what I need to do,  but it does not seem to work in
> > > common lisp.  I get the error:
> > 
> > There is no way of doing it in CL (unless you modify your code to
> > check the time periodically).  You need some kind of `monitor' which
> > watches the process doing the calculation and stops it when it has
> > had too long.
> 
> Here is a version for LispWorks:
> 
> (defmacro with-timeout ((timeout &optional result) &body body)
>   (let ((proc (gensym "PROC"))
>         (blockname (gensym "BLOCKNAME")))
>     `(block ,blockname
>        (let ((,proc (mp:process-run-function
>                      (format nil "Timeout Function for ~A"
>                              (mp:process-name mp:*current-process*))
>                      (list :priority (mp:process-priority
>                                       mp:*current-process*))
>                      (lambda (,proc)
>                        (sleep ,timeout)
>                        (mp:process-interrupt ,proc
>                                              (lambda ()
>                                                (return-from ,blockname
>                                                             ,result))))
>                      mp:*current-process*)))
>          (unwind-protect
>              (progn
>                ,@body)
>            (mp:process-kill ,proc))))))
> 
> I didn't test much but it seems to work fine...
> 
> Regards,
From: Edi Weitz
Subject: Re: Breaking Loops or Evals?
Date: 
Message-ID: <87llzmu114.fsf@bird.agharta.de>
····@geneseo.edu (Nonzero) writes:

> Thanks for the help.
> 
> I tried your code, but got the familiar 
> 
> *** - READ from #<INPUT BUFFERED FILE-STREAM CHARACTER #P"test.lisp"
> @7>: there is no package with name "MP"
> 
> Error.
> 
> I'm using CLISP.  I don't know if that can help anyone help me :)

The current release versions of CLISP don't have MP support as far as
I know.

You can get a trial version of Xanalys LispWorks (the CL
implementation Nils used in his example) from

  <http://www.lispworks.com/downloads/lw-personal-edition.html>

If you want a "free" (whatever that means) Lisp, CMUCL
<http://www.cons.org/cmucl/> also has MP support (at least for X86). I
think the EncyCMUCLopedia at

  <http://cvs2.cons.org/ftp-area/cmucl/doc/EncyCMUCLopedia/>

has some documentation for it.

Edi.
From: Frode Vatvedt Fjeld
Subject: Re: Breaking Loops or Evals?
Date: 
Message-ID: <2hptozs2d4.fsf@vserver.cs.uit.no>
····@geneseo.edu (Nonzero) writes:

> Any ideas or suggestions?

Use an implementation that implements mp:with-timeout. This operator
is not a part of Common Lisp per se.

-- 
Frode Vatvedt Fjeld