Title: | Process Synchronization Using File Locks |
---|---|
Description: | Implements synchronization between R processes (spawned by using the "parallel" package for instance) using file locks. Supports both exclusive and shared locking. |
Authors: | Ivan Popivanov |
Maintainer: | Ivan Popivanov <[email protected]> |
License: | Apache License 2.0 |
Version: | 0.7 |
Built: | 2025-03-13 03:45:29 UTC |
Source: | https://github.com/ivannp/flock |
Enables synchronization between R processes using a file lock. Supports both exclusive (writer) and shared (readers) locks.
On UNIX, the implementation relies on the fcntl
system
call. While on Windows, the LockFileEx
/UnlockFileEx
APIs are used.
Package: | flock |
Type: | Package |
Version: | 0.6 |
Date: | 2014-11-24 |
License: | Apache License 2.0 |
Ivan Popivanov
Maintainer: Ivan Popivanov <[email protected]>
## Not run: require(DBI) require(flock) require(RSQLite) require(parallel) dbpath <- tempfile() con <- dbConnect(RSQLite::SQLite(), dbname=dbpath) df <- data.frame(value = 0) dbWriteTable(con, "test", df) dbDisconnect(con) write.one.value <- function(val, lock.name=NULL) { if(!is.null(lock.name)) { file.lock = lock(lock.name) } # The three lines below are the "critical section" con <- dbConnect(RSQLite::SQLite(), dbname = dbpath) dbWriteTable(con, "test", data.frame(value = val), append = TRUE) dbDisconnect(con) if(!is.null(lock.name)) { unlock(file.lock) } } lock.name = tempfile() # Run the parallel database updates with two cores mclapply(1:100, write.one.value, mc.cores=2, lock.name=lock.name) # To see the failing scenario, run (on a multi-core system): # mclapply(1:100, write.one.value, mc.cores=2) ## End(Not run)
## Not run: require(DBI) require(flock) require(RSQLite) require(parallel) dbpath <- tempfile() con <- dbConnect(RSQLite::SQLite(), dbname=dbpath) df <- data.frame(value = 0) dbWriteTable(con, "test", df) dbDisconnect(con) write.one.value <- function(val, lock.name=NULL) { if(!is.null(lock.name)) { file.lock = lock(lock.name) } # The three lines below are the "critical section" con <- dbConnect(RSQLite::SQLite(), dbname = dbpath) dbWriteTable(con, "test", data.frame(value = val), append = TRUE) dbDisconnect(con) if(!is.null(lock.name)) { unlock(file.lock) } } lock.name = tempfile() # Run the parallel database updates with two cores mclapply(1:100, write.one.value, mc.cores=2, lock.name=lock.name) # To see the failing scenario, run (on a multi-core system): # mclapply(1:100, write.one.value, mc.cores=2) ## End(Not run)
Checks whether a lock has been obtained.
is.locked(file.lock)
is.locked(file.lock)
file.lock |
The lock as an object of type |
Ivan Popivanov
## Not run: require(flock) file.lock = lock("~/file.lock") # Critical section code goes between here and the unlock call if(is.locked(file.lock)) { print("Got the lock!") } unlock(file.lock) ## End(Not run)
## Not run: require(flock) file.lock = lock("~/file.lock") # Critical section code goes between here and the unlock call if(is.locked(file.lock)) { print("Got the lock!") } unlock(file.lock) ## End(Not run)
Locks a file in exclusive or shared mode.
lock(path, exclusive = TRUE)
lock(path, exclusive = TRUE)
path |
Character. The path. |
exclusive |
Logical. The lock type, exclusive or shared. |
The file is created if it doesn't exist.
Returns an object of type FileLock
, which is to be
used for the unlock
call.
Ivan Popivanov
## Not run: require(flock) file.lock = lock("~/file.lock") # Critical section code goes here unlock(file.lock) ## End(Not run)
## Not run: require(flock) file.lock = lock("~/file.lock") # Critical section code goes here unlock(file.lock) ## End(Not run)
Unlocks a file previously locked via lock
.
unlock(file.lock)
unlock(file.lock)
file.lock |
The |
Ivan Popivanov
## Not run: require(flock) file.lock = lock("~/file.lock") # Critical section code goes here unlock(file.lock) ## End(Not run)
## Not run: require(flock) file.lock = lock("~/file.lock") # Critical section code goes here unlock(file.lock) ## End(Not run)