Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semaphore (IPC) in R

Tags:

r

semaphore

ipc

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?

  1. 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.

  2. Solution should not only work for 'parallel' processing within one R session, but also for totally separate R sessions (separate independent processes).

  3. 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.

like image 954
Tomas Avatar asked Oct 12 '25 12:10

Tomas


1 Answers

Use boost.mutex from synchronicity (if you want only 1 process in the critical section at a time)

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

like image 67
SargeATM Avatar answered Oct 14 '25 08:10

SargeATM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!