[R] Auto-killing processes spawned by foreach::doMC

Henrik Bengtsson hb at biostat.ucsf.edu
Tue Nov 9 06:59:04 CET 2010


I just stumbled into the 'fork' package (GPL-2).  It allows you do
send signals from within R.  Unfortunately it is not available on
Windows, but it is definitely a start.

Here is an example how an R session can send an interrupt signal
(SIGINT) to itself:

library("fork");

# Get the process ID of the current R session
pid <- getpid();

# Run some code and interrupt the current R session
tryCatch({
  print("Tic");
  Sys.sleep(2);
  print("Tac");
  kill(pid, signal=sigval("SIGINT")$val);
  for (kk in 1:100) { print(kk); }
}, interrupt=function(int) {
  print(int);
})

which gives:

[1] "Tic"
[1] "Tac"
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12
<interrupt: >

Interestingly, the SIGINT signal is not interrupting R momentarily,
which is why the code following kill() is still executed for a while
before the interrupt is caught.

/Henrik

On Wed, Nov 3, 2010 at 5:06 PM, Henrik Bengtsson <hb at biostat.ucsf.edu> wrote:
> Hi,
>
> I am also interest in ways to in R send signals to other R
> sessions/processes, ideally in (what appears to be) an OS-independent
> way.  For what it is worth, related question have been asked before,
> cf. R-devel thread 'Sending signals to current R process from R
> running under MS Windows (c.f. Esc)' started on 2009-11-28:
>
> http://www.mail-archive.com/r-devel@r-project.org/msg18790.html
>
> Still no workable suggestions/solutions AFAIK.
>
> /Henrik
>
> On Wed, Nov 3, 2010 at 2:55 PM, Steve Lianoglou
> <mailinglist.honeypot at gmail.com> wrote:
>> Hi all,
>>
>> Sometimes I'll find myself "ctrl-c"-ing like a madman to kill some
>> code that's parallelized via foreach/doMC when I realized that I just
>> set my cpu off to do something boneheaded, and it will keep doing that
>> thing for a while.
>>
>> In these situations, since I interrupted its normal execution,
>> foreach/doMC doesn't "clean up" after itself by killing the processes
>> that were spawned. Furthermore, I believe that when I quit my "main" R
>> session, the spawned processes still remain (there, but idle).
>>
>> I can go in via terminal (or some task manager/activity monitor) and
>> kill them manually, but does foreach (or something else (maybe
>> multicore?)) keep track of the process IDs that it spawned?
>>
>> Is there some simple doCleanup() function I can write to get these R
>> processes and kill them automagically?
>>
>> For what it's worth, I'm running on linux & os x, R-2.12 and the
>> latest versions of foreach/doMC/multicore (though, I feel like this
>> has been true since I've started using foreach/doMC way back when).
>>
>> Thanks,
>> -steve
>>
>> --
>> Steve Lianoglou
>> Graduate Student: Computational Systems Biology
>>  | Memorial Sloan-Kettering Cancer Center
>>  | Weill Medical College of Cornell University
>> Contact Info: http://cbio.mskcc.org/~lianos/contact
>>
>> ______________________________________________
>> 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.
>>
>



More information about the R-help mailing list