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

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
150
pub points
81%
popularity

Publisher

verified publisherharuka39.me

Synchronization primitives for asynchronous Dart code, inspired from Python

Repository (GitHub)
View/report issues

Documentation

API reference

License

GPL-3.0 (LICENSE)

More

Packages that depend on async_locks