[R] The R "fork"

Henrik Bengtsson hb at maths.lth.se
Sat Mar 18 19:01:15 CET 2006


On 3/18/06, Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:
> pau carre wrote:
> > Hello, I would like to call a function that can take infinite time to
> > be executed in some circumstances (which can not be easily detected).
> > So, I would like that once the function is being executed for more
> > than two seconds it is stopped. I have found documentation for timers
> > but i did not found how to "kill" a function. Moreover, I would like
> > not to change the function code (it should work in all R versions and
> > I want to use the default function because of the updates in its
> > package).
>
>
> On the one hand I do not think this is possible on all supported
> platforms, on the other hand I think non-terminating algotithms can be
> improved by checking number of iterations or recursion depth for some
> forces termination.
> You might want to ask the package maintainer to include such an
> improvement (of not bugfix) in his code. The package maintainer
> certainly will be happy to see your contribution.

In addition, you could ask to have hooks added, see ?seeHook. Then you
could write a hook function that generates an error, e.g. stop(),
which you then can catch with tryCatch(). If you define your own error
class, say, TimeoutError, your calling code will look like

 tryCatch(neverEndingFunction(), TimeoutError=function(ex) print(ex))


Alternatively, you could send a SIGINT (corresponds to Ctrl-C) signal
to R from "outside" R. On Unix you can do something like:

pkill -INT -U $user R

from a shell. I do not know how to send a SIGINT on Windows (the
Windows application 'termkill' will just kill R if you try that).  In
R you catch this interrupt signal via

 tryCatch(neverEndingFunction(), interrupt=function(intr) print(intr))

Then interrupt will exit neverEndingFunction() and continue with the
next line of code.  If you can schedule or write a shell script that
sends the SIGINT signal every two minutes this would work for you.  It
is possible that you may be able to do all of this from within R;
write Tcl(Tk) code, which R supports, that calls 'pkill' at certain
times.  I think this is possible even though R is single threaded, but
I'm not a Tcl hacker so you have to ask someone else about this.

Interrestingly, the last few days I've tried to find a way for a
function to send an interrupt itself.  See r-help thead "New
simpleExit condition" (Mar 14-Mar 16) for details.  Solving that would
be one step closer to solve your problem.  (No, system("pkill -INT
...") does not seem to work).

Cheers

Henrik


> Uwe Ligges
>
>
> > Pau
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list