I would like to start two RStudio instances in the same PC and run the same script with GRASS commands in each of them simultaneously. This is to process faster a long list of spatial data.
The script I have writen runs fine if I only have one Rstudio open, but if I open two of them, as I intend, one of them falls into an error such as this:
My interpretation is that both instances attempt using the same "environment" (mapset?), which causes a conflict when some of the files that are writen or deleted from one of the instances but needs to be accessed by the other (because they share filenames?). I have tried to avoid that pitfall by playing with working directories and explicitly defining user-specific tmp directoties. I also tried changing the names of "mapset", but I've come to the conclusion that this needs to be left as "PERMANENT". None of that was able to solve the error. This is a simplified version of my code at the moment (but note I've tried simpler versions, i.e., using tempdir()):
for (roads_r in rasters){ # roads_r is a rasterized lines object, value 1 where roads, rest is NA
# built a random name (I'm aware I could use tempdir(), but explored this to have files at hand
tmp_custom <- paste0("tmp", paste0(sample(1:9, 1), sample(1:9, 1), sample(1:9, 1), sample(1:9, 1)))
tempDir <- paste0(getwd(), "/tmp/", tmp_custom)
rgrass7::use_sp()
dir.create(tempDir, recursive = TRUE, showWarnings = FALSE)
## Initiate GRASS environment
rgrass7::initGRASS(gisBase = grassDir,
home = tempDir, # I'm using the custom temp Dir created above
gisDbase = tempDir, # I'm using the custom temp Dir created above
location = basename(tempDir),
mapset = "PERMANENT", # I had to stick to PERMANENT for my code to work
override = TRUE,
remove_GISRC = TRUE)
## Prepare my raster and read it to GRASS environment
resolution <- as.character(raster::res(roads_r)[1])
proj4 <- raster::projection(roads_r)
ext <- raster::extent(roads_r)
xmax <- as.character(ext@xmax); xmin <- as.character(ext@xmin); ymax <- as.character(ext@ymax); ymin <- as.character(ext@ymin)
rgrass7::execGRASS("g.proj", flags = c("c", "quiet"),
proj4 = proj4)
rgrass7::execGRASS("g.region", flags = c("quiet"),
n = ymax, s = ymin, e = xmax, w = xmin, res = resolution)
rgrass7::use_sp()
rastSGDF <- methods::as(raster(roads_r), "SpatialGridDataFrame")
rgrass7::writeRAST(rastSGDF,
vname = "roads_r",
overwrite = TRUE)
## Calculate distance - This is what I actually need
execGRASS("r.grow.distance",
input = "roads_r",
distance = "distance",
metric = "geodesic",
flags = c("quiet", "overwrite", "m"))
## Retrieve result
roads_dist_raster <- readRAST("distance") %>%
raster()
}
Note that this is my attempt after exploring the fasterRaster
R package and fasterRastDistance()
function (see Explicit temp files in rgrass7 / fasterRaster to avoid interferences when running simultaneous R sessions), following advice from the first reply there. I suspect that the pitfall where I am know is likely the same I was in before.
How could I solve this?
parallel
orforeach
?