async_locks 1.1.1 copy "async_locks: ^1.1.1" to clipboard
async_locks: ^1.1.1 copied to clipboard

outdated

Synchronization primitives for asynchronous Dart code, inspired from Python

example/example.dart

/// Synchronization to avoid concurrent write to a file
import "dart:io";

import "package:async_locks/async_locks.dart";

class Program {
  final lock = Lock();

  Future<void> runFuture(int n) async {
    var file = File("example.txt");
    await lock.run(() async => await file.writeAsString("Writing from Future-$n\n", mode: FileMode.append, flush: true));
  }

  Future<void> run() async {
    await Future.wait([runFuture(1), runFuture(2), runFuture(3), runFuture(4)]);
    var file = File("example.txt");
    var content = await file.readAsString();
    print(content);
  }
}

void main() async {
  // Create example file
  var file = File("example.txt");
  await file.writeAsString("EXAMPLE FILE\n");

  var program = Program();
  await program.run();
}
7
likes
0
pub points
84%
popularity

Publisher

verified publisherharuka39.me

Synchronization primitives for asynchronous Dart code, inspired from Python

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on async_locks