Is there a way to share a lock (like a lock file) between R processes?

I have a bunch of different R-processes (regardless of starting from the command line) that need to load different large files. To avoid clogging the network, I want to add a lock / semaphore, for example. through the lock file so that they receive their file one by one. Only one process should be able to obtain a lock on a standard Linux system.

+6
source share
2 answers

While I could not find the R package, there is a Linux lockfile that can be used:

 write("Attempting to get lock", stderr()) system("lockfile /tmp/my_simple_lock") # Do stuff write("Releasing lock", stderr()) system("rm -f /tmp/my_simple_lock") 
+3
source

Perhaps a more explicit version of the previous answer. If the file you are accessing is called filename, and you are working in the same directory ... (if you use different directories, then obviously replace the file name with the whole path)

 write("Attempting to get lock", stderr()) system("lockfile filename.lock") \# do whatever you want to the file write("Releasing lock", stderr()) remove.file("filename.lock") 
+1
source

All Articles