[R] multiple concurrent write in R

William Dunlap wdunlap at tibco.com
Wed Oct 30 22:13:11 CET 2013


On Linux, at least, you can have various processes write into the same file, by opening
it with "r+" mode and calling seek() to position the file pointer before writing.   E.g.,

> library(parallel)
> cl4 <- makeCluster(4)
> tf <- tempfile()
> cat(rep("--------", 2*length(cl4)), sep="\n", file=tf)
> readLines(tf)
[1] "--------" "--------" "--------" "--------" "--------" "--------" "--------"
[8] "--------"
> z <- parLapply(cl4, 8:1, function(i, tf){
                                                 f <- file(tf, open="r+")
                                                 on.exit(close(f))
                                                 seek(f, (i-1)*9, rw="w") 
                                                 ret <- c(i, Sys.getpid())
                                                 cat(ret, file=f); ret},
                                               tf=tf)
> readLines(tf)
[1] "1 22406-" "2 22406-" "3 22397-" "4 22397-" "5 22388-" "6 22388-" "7 22379-"
[8] "8 22379-"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Jeff Newmiller
> Sent: Wednesday, October 30, 2013 12:58 PM
> To: Zhifa Liu; r-help at r-project.org
> Subject: Re: [R] multiple concurrent write in R
> 
> I think the answer is no. Use the master process to manage IO.
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                       Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
> 
> Zhifa Liu <zhifaliu at gmail.com> wrote:
> >I have over 200  CPUs could write to the same file at the same time,
> >does
> >someone know how to handle the multiple concurrent write in R?
> >
> >	[[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.



More information about the R-help mailing list