FileLock: The FileLock class

Description Usage Arguments Details Fields and Methods Author(s) References Examples

Description

Package: R.synchronize
Class FileLock

Object
~~|
~~+--FileLock

Directly known subclasses:
NullFileLock

public static class FileLock
extends Object

The FileLock class provides method for locking and releasing files.

Usage

1
FileLock(con=NULL, pathname=NULL, pathnameL=NULL, ..., .core=TRUE)

Arguments

con

A connection.

pathname, pathnameL

Pathnames.

...

Not used.

.core

Internal only.

Details

This class is instantiated via static methods *tryLock() and *lock(). It should never be instantiated via the constructor.

Fields and Methods

Methods:

as.character -
getConnection -
getPathname -
isLocked -
lock -
release -
tryLock -

Methods inherited from Object:
$, $<-, [[, [[<-, as.character, attach, attachLocally, clearCache, clone, detach, equals, extend, finalize, gc, getEnvironment, getFields, getInstantiationTime, getStaticInstance, hasField, hashCode, ll, load, objectSize, print, save

Author(s)

Henrik Bengtsson (http://www.braju.com/R/)

References

[1] http://en.wikipedia.org/wiki/File_locking

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# The file to be locked (note, it does not have to exists)
pathname <- "foo.txt"

# Try to lock the above file
lockA <- FileLock$tryLock(pathname)
print(lockA)

# Try to lock it again (should not work)
lockB <- FileLock$tryLock(pathname)
print(lockB)

# Release the lock
release(lockA)

# Try to lock again
lockC <- FileLock$lock(pathname);
print(lockC)

# Oops, we forget to release...
rm(lockC)

# However, the garbage collector will release it,
# which is forced to run whenever the file appears
# to be locked.  This is why the following works.

# Try to lock it
lockA <- FileLock$tryLock(pathname)
print(lockA)

# Try to lock it again (should not work)
lockB <- FileLock$tryLock(pathname)
print(lockB)

# Clean up and remove any stray file locks
rm(lockA, lockB)
gc()

R.synchronize documentation built on May 2, 2019, 5:46 p.m.