

Dear all,
I often need to execute functions repeatedly (thousands or more times).
While doing so, I encounter two types of problems:
1.) In some models, the estimation process fails due to convergence problems
2.) Some models will run forever.
My solution to #1 is to use tryCatch around the function call so that my
script does not stop if one of the models will raise an error. This works
fine.
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.
I guess a bruteforce solution would be to start a new R process for each
model from my R script and to kill the process using system commands after a
given time. However, restarting the R interpreter each time sounds very
inelegant. Are there any versions that work within a single script? Or
alternative suggestions?
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.
Any help appreciated!
Kind regards,
Lukas
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


?help.search("time")
?proc.time
?system.time
You can stick these in your code appropriately to keep track of elapsed
time. You could also count iterations, of course.
 Bert Gunter
Genentech
Original Message
From: [hidden email] [mailto: [hidden email]] On
Behalf Of Lukas Rode
Sent: Wednesday, April 02, 2008 8:07 AM
To: [hidden email]
Subject: [R] Stopping a function execution automatically after a given time
Dear all,
I often need to execute functions repeatedly (thousands or more times).
While doing so, I encounter two types of problems:
1.) In some models, the estimation process fails due to convergence problems
2.) Some models will run forever.
My solution to #1 is to use tryCatch around the function call so that my
script does not stop if one of the models will raise an error. This works
fine.
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.
I guess a bruteforce solution would be to start a new R process for each
model from my R script and to kill the process using system commands after a
given time. However, restarting the R interpreter each time sounds very
inelegant. Are there any versions that work within a single script? Or
alternative suggestions?
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.
Any help appreciated!
Kind regards,
Lukas
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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.
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 < [hidden email]> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide
> http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
[[alternative HTML version deleted]]
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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.
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 < [hidden email]> 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
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide
>> http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
>
>
> 
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


You should look at AutoIt or Autohotkey for this.
Best,
Philippe Grosjean
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.
>
> 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 < [hidden email]> 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
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/rhelp>>> PLEASE do read the posting guide
>>> http://www.Rproject.org/postingguide.html>>> and provide commented, minimal, selfcontained, reproducible code.
>>>
>> [[alternative HTML version deleted]]
>>
>>
>>
>> 
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


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 longrunning code is predominantly R (and not compiled C or
Fortran) and is running from Rgui or interative Rterm (unlikely) then
pressing ESC/CtrlC 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 nonimplementation 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 multiCPU 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 < [hidden email]> 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
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/rhelp>>> PLEASE do read the posting guide
>>> http://www.Rproject.org/postingguide.html>>> and provide commented, minimal, selfcontained, reproducible code.
>>>
>>
>> [[alternative HTML version deleted]]
>>
>>
>>
>> 
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/rhelp> PLEASE do read the posting guide http://www.Rproject.org/postingguide.html> and provide commented, minimal, selfcontained, reproducible code.
>

Brian D. Ripley, [hidden email]
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 ______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.


For information, the Rdevel version of R now has an experimental function
setTimerLimit() to set time limit(s) (elapsed and/or CPU) for each
toplevel 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 longrunning code is predominantly R (and not compiled C or
> Fortran) and is running from Rgui or interative Rterm (unlikely) then
> pressing ESC/CtrlC 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 nonimplementation 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 multiCPU 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 < [hidden email]> 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
>>>>
>>>> ______________________________________________
>>>> [hidden email] mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/rhelp>>>> PLEASE do read the posting guide
>>>> http://www.Rproject.org/postingguide.html>>>> and provide commented, minimal, selfcontained, reproducible code.
>>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>>
>>>
>>> 
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/rhelp>>> PLEASE do read the posting guide
>>> http://www.Rproject.org/postingguide.html>>> and provide commented, minimal, selfcontained, reproducible code.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/rhelp>> PLEASE do read the posting guide
>> http://www.Rproject.org/postingguide.html>> and provide commented, minimal, selfcontained, reproducible code.
>>
>
> 
> Brian D. Ripley, [hidden email]
> 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, [hidden email]
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 ______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/rhelpPLEASE do read the posting guide http://www.Rproject.org/postingguide.htmland provide commented, minimal, selfcontained, reproducible code.

