nabble.30.miller_2555 at spamgourmet.com
2009-Jul-11 00:16 UTC
[R] Passing arguments to forked children
Hi - I have attempted to use the fork::fork() function to perform parallel processing. However, the child R function called needs to know a given set of parameters to complete its task. Specifically, I iterate through a vector, and output values based on the elements of that vector to a database. The output strings contain elements of the iterated vector. I mocked-up the following code as an example (NOTE: WHILE NOT SPECIFICALLY DANGEROUS, THIS CODE MAXED OUT THE LIMIT OF MY SYSTEMS FORKS -- this means that, if you run this code, no additional processes on your system may start until you kill the parent R session! BE VERY CAREFUL IF YOU DECIDE TO EXECUTE THIS CODE -- obviously, I do not recommend it. Presumably, an infinite recursion scenario arose so you are just left with a *lot* of R sessions. Also, as each R session has equal access to stdin, you cannot reliably type commands into a given R session to terminate it -- so definitely don't run in a CLI environment -- at least you can kill the parent window running R in a GUI environment). In any case, here is the code: # -- BEGIN CODE library("fork"); myforksub <- function(mymsg='default') { cat(mymsg,sep='\n'); exit(); } myforkparent <- function(n=10, mymsg='') { mypid <- c(); for (i in 1:n) { mypid <- c(mypid, fork(myforksub(mymsg))); } # wait(NULL) apparently does not wait for all children to finish for (i in 1:n) { wait(mypid[i]); } } myforkparent(mymsg='new'); # -- END CODE Obviously, 'fork(myforksub)' will work fine, but myforksub cannot access the mymsg variable containing the 'new' value. How can I amend the above without having to resort to socket connections to pass information? While this question is specific to the 'fork' non-standard package, I thought a few people here would be familiar enough with its use to offer a suggestion or two. Thanks!
On Fri, Jul 10, 2009 at 8:16 PM, <nabble.30.miller_2555 at spamgourmet.com> wrote:> Hi - > > ? ?I have attempted to use the fork::fork() function to perform > parallel processing. However, the child R function called needs to > know a given set of parameters to complete its task. Specifically, I > iterate through a vector, and output values based on the elements of > that vector to a database. The output strings contain elements of the > iterated vector. I mocked-up the following code as an example (NOTE: > WHILE NOT SPECIFICALLY DANGEROUS, THIS CODE MAXED OUT THE LIMIT OF MY > SYSTEMS FORKS -- this means that, if you run this code, no additional > processes on your system may start until you kill the parent R > session! BE VERY CAREFUL IF YOU DECIDE TO EXECUTE THIS CODE -- > obviously, I do not recommend it. Presumably, an infinite recursion > scenario arose so ?you are just left with a *lot* of R sessions. Also, > as each R session has equal access to stdin, you cannot reliably type > commands into a given R session to terminate it -- so definitely don't > run in a CLI environment -- at least you can kill the parent window > running R in a GUI environment). In any case, here is the code: > > # -- BEGIN CODE > library("fork"); > > myforksub <- function(mymsg='default') { > ? ? ? ?cat(mymsg,sep='\n'); > ? ? ? ?exit(); > } > > myforkparent <- function(n=10, mymsg='') { > ? ? ? ?mypid <- c(); > ? ? ? ?for (i in 1:n) { > ? ? ? ? ? ? ? ?mypid <- c(mypid, fork(myforksub(mymsg))); > ? ? ? ?} > ? ? ? ?# wait(NULL) apparently does not wait for all children to finish > ? ? ? ?for (i in 1:n) { > ? ? ? ? ? ? ? ?wait(mypid[i]); > ? ? ? ?} > } > > myforkparent(mymsg='new'); > > # -- END CODE > > Obviously, 'fork(myforksub)' will work fine, but myforksub cannot > access the mymsg variable containing the 'new' value. How can I amend > the above without having to resort to socket connections to pass > information?Like so: mypid <- c(mypid, fork(function () { myforksub(sprintf("Hi, I'm child %d", i)) })); BTW: # wait(NULL) apparently does not wait for all children to finish wait(NULL) traditionally waits for any single child to finish, not for all. - Godmar