Is there any way to use semaphores (IPC) in R? I have several R scripts running in paralel and I want only certain (smaller) number of them to enter the critical stage of the script. E.g. in C on Linux, this would be easily accomplished by named semaphores shared between processes, but how to do this in R?
Solution should work on MS Windows as well. For example, bettermc::sem_open()
is not supported on Windows. It looks like Windows support semaphores, so it should be possible. It also looks like named semaphores should also work on Windows for the case of communication between different processes.
Solution should not only work for 'parallel' processing within one R session, but also for totally separate R sessions (separate independent processes).
I found https://github.com/gbenison/R-semaphore but I didn't test this, it doesn't seem to be on CRAN; I prefer solution which is available within CRAN packages already.
In any process that has a critical section, do the following.
m = boost.mutex('global_name_of_my_mutex')
lock(m)
# Critical section of code
unlock(m)
The R package synchronicity is available by running:
install.packages('synchronicity')
It provides boost.mutex
which is an R wrapper around C++ boost's named_upgradable_mutex
.
https://www.rdocumentation.org/packages/synchronicity/versions/1.3.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With