I'd like to have two programs (running on different Dart VM) writing data to the same file (appending actually). However, I can't find lock or similar mechanism to avoid racing. Any suggestion? Thanks.
File locking was just added. (http://dartbug.com/17045 was changed to fixed)
Copied example form https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/tests/standalone/io/file_lock_script.dart?spec=svn42733&r=42733
import "dart:io";
main(List<String> args) {
File file = new File(args[0]);
int start = null;
int end = null;
var mode = FileLock.EXCLUSIVE;
if (args[1] == 'SHARED') {
mode = FileLock.SHARED;
}
if (args[2] != 'null') {
start = int.parse(args[2]);
}
if (args[3] != 'null') {
end = int.parse(args[3]);
}
var raf = file.openSync(mode: WRITE);
try {
raf.lockSync(mode, start, end);
print('LOCK SUCCEEDED');
} catch (e) {
print('LOCK FAILED');
} finally {
raf.closeSync();
}
}
See also https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/tests/standalone/io/file_lock_test.dart?spec=svn42733&r=42733 for a more extensive example.
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