Hi, is there a (cross-platform) file-locking mechanism available in R (or via some package)? I am looking for a way to have one R session lock a file for read/write access, while being updated/modified by another R session. This will provide me with a-poor-mans parallelization method. It is ok to have so called advisory looking (as in Unix), which are non-mandatory to follow. If not available, I'll use lock files, but there are some potential problems in creating such in an atomic way. Ideally I wish to have this working on all platforms. Cheers Henrik
[Henrik Bengtsson]>I am looking for a way to have one R session lock a file for >read/write access, while being updated/modified by another R session. >This will provide me with a-poor-mans parallelization method. It is >ok to have so called advisory looking (as in Unix), which are >non-mandatory to follow. If not available, I'll use lock files, but >there are some potential problems in creating such in an atomic way. >Ideally I wish to have this working on all platforms.Something which worked for me, so far, is to create a directory, and see if the creation succeeded or failed. If the creation succeeded, you have the lock. If it failed, something else created it and has the lock. You then keep retrying after some delay. Of course, the directory should not be used outside your locking protocol. On the few platforms I used, but likely on most of them, creating a directory "atomically" ought to be safe. Yet, this might be a bit heavy-weighted when locks are meant to be light and quick. If you want some more sophistication, leave a file within the lock directory (once you successfully created it and so, acquired the lock), telling who you are. Destroy the file before destroying the directory (of course). More typically on Unix, put in this file the id of the running process, so when creating the directory fails, the attempter may look in the file, and if there is no such process running, consider the lock stale, and preempt it (to do this safely requires some thinking). -- Fran?ois Pinard http://pinard.progiciels-bpi.ca
On Mon, 2 Jul 2007, Henrik Bengtsson wrote:> Hi, > > is there a (cross-platform) file-locking mechanism available in R (or > via some package)?I don't believe there really is a cross-platform file-locking mechanism available to any language. File-locking is an OS feature, and the semantics differ. For those unfamiliar with this, the Wikipedia article is a good start (but ignores the POSIX lockf interface).> I am looking for a way to have one R session lock a file for > read/write access, while being updated/modified by another R session. > This will provide me with a-poor-mans parallelization method. It is > ok to have so called advisory looking (as in Unix), which are > non-mandatory to follow. If not available, I'll use lock files, but > there are some potential problems in creating such in an atomic way.Depends what you mean by 'atomic'. In R, the only way to have non-interruptible operations is via .Call or similar: the evaluator is interruptible at all times so it seems that this issue applies equally to all file locking from R.> Ideally I wish to have this working on all platforms. > > Cheers > > Henrik > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595