[R] Stopping a function execution automatically after a given time
Prof Brian Ripley
ripley at stats.ox.ac.uk
Sat Apr 5 15:18:05 CEST 2008
For information, the R-devel version of R now has an experimental function
setTimerLimit() to set time limit(s) (elapsed and/or CPU) for each
top-level function. It will be a while before (or even if) it is
released, but it may prove useful to those with an immediate need.
On Wed, 2 Apr 2008, Prof Brian Ripley wrote:
> On Wed, 2 Apr 2008, Duncan Murdoch wrote:
>
>> On 4/2/2008 12:00 PM, Lukas Rode wrote:
>>> Dear Bert and Mel,
>>>
>>> thanks for your help, but I'm afraid this doesn't solve my problem.
>>>
>>> As I wrote in my previous mail (cf quote below) in most cases I will not
>>> be
>>> able to modify the code of the function that I want to run. This is why I
>>> was asking for a wrapper solution similar to what tryCatch does. I have
>>> hinted at a very inelegant version that generates a new R process for each
>>> function run and kills the process after a given time. But I'm sure there
>>> must be something more elegant. I hope I have been clear enough in my
>>> problem description.
>>
>> On some systems (not Windows) you could ask some external process to
>> send a signal after a certain time interval, and I believe you can write
>> you code to recover afterwards. I don't know any reasonable way to do
>> this on Windows.
>
> The problem is that only drastic signals will get listened to, and I don't
> believe they allow you to do anything other than save your work. SIGKILL,
> SIGTERM, SIGUSR1 and SIGUSR2 are those likely to work (see ?Signals).
>
> Philippe Grosjean said
>
>> You should look at AutoIt or Autohotkey for this.
>
> I don't see how that helps. R will not respond to keys when running a
> function, including the interrrupt key, unless written to do so. So on
> Windows if the long-running code is predominantly R (and not compiled C or
> Fortran) and is running from Rgui or interative Rterm (unlikely) then
> pressing ESC/Ctrl-C should interrupt fairly soon.
>
> But almost certainly one is using a batch process. You can kill that from an
> external process on Windows (but not via SIGUSR1/2, although I think that is
> a non-implementation detail).
>
>
> I do this by running multiple processes and setting a CPU limit on them from
> the shell used to launch them. By using 'make -j' I can control how many run
> at once (important on multi-CPU machines).
>
>
>>
>> Duncan Murdoch
>>
>>>
>>> Thanks again,
>>> Lukas
>>>
>>> Here is what I wrote before:
>>> Note that mostly these functions are not written by me and not R code
>>> (like
>>> nlme for example), so it is not feasible to adapt the function itself.
>>> Rather, it needs to be a wrapper around the function, similar to tryCatch.
>>>
>>>
>>>
>>> On Wed, Apr 2, 2008 at 5:23 PM, mel <mel at altk.com> wrote:
>>>
>>>> Lukas Rode a écrit :
>>>>
>>>>> Nowever, with regard to #2, I am lost. I would like to set a maximum
>>>> time
>>>>> limit (say, 1 minute) and if my procedure is still running then, I would
>>>>> like to move on to the next model.
>>>>
>>>>
>>>> begin_time = as.difftime(format(Sys.time(), '%H:%M:%S'), units='secs');
>>>> for(...)
>>>> {
>>>> ...
>>>> current_time = as.difftime(format(Sys.time(), '%H:%M:%S'),
>>>> units='secs');
>>>> delay = current_time - begin_time;
>>>> if (delay>60) return();
>>>> }
>>>>
>>>> a counter may also be enough
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>>
>>>
>>> ------------------------------------------------------------------------
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> --
> Brian D. Ripley, ripley at stats.ox.ac.uk
> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel: +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272866 (PA)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list