semaphore 0.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 88

semaphore #

Semaphore is lightweight data type that is used for controlling the cooperative access to a common resource inside the isolate.

Version: 0.1.1

Example: #

import 'dart:async';

import 'package:semaphore/semaphore.dart';

Future<void> main() async {
  var maxCount = 3;
  var running = <int>[];
  var simultaneous = 0;
  var sm = new LocalSemaphore(maxCount);
  var tasks = <Future>[];
  for (var i = 0; i < 9; i++) {
    tasks.add(new Future(() async {
      try {
        await sm.acquire();
        running.add(i);
        if (simultaneous < running.length) {
          simultaneous = running.length;
        }

        print("Start $i, running $running");
        await _doWork(100);
        running.remove(i);
        print("End   $i, running $running");
      } finally {
        sm.release();
      }
    }));
  }

  await Future.wait(tasks);
  print("Max permits: $maxCount, max simultaneous runned: $simultaneous");
}

Future _doWork(int ms) {
  // Simulate work
  return new Future.delayed(new Duration(milliseconds: ms));
}

Output:

Start 0, running [0]
Start 1, running [0, 1]
Start 2, running [0, 1, 2]
End   0, running [1, 2]
Start 3, running [1, 2, 3]
End   1, running [2, 3]
Start 4, running [2, 3, 4]
End   2, running [3, 4]
Start 5, running [3, 4, 5]
End   3, running [4, 5]
Start 6, running [4, 5, 6]
End   4, running [5, 6]
Start 7, running [5, 6, 7]
End   5, running [6, 7]
Start 8, running [6, 7, 8]
End   6, running [7, 8]
End   7, running [8]
End   8, running []
Max permits: 3, max simultaneous runned: 3

0.1.1 #

  • Performed the modifications according to "pub.dartlang.org" suggestions.

example/example.dart

import 'dart:async';

import 'package:semaphore/semaphore.dart';

Future<void> main() async {
  var maxCount = 3;
  var running = <int>[];
  var simultaneous = 0;
  var sm = new LocalSemaphore(maxCount);
  var tasks = <Future>[];
  for (var i = 0; i < 9; i++) {
    tasks.add(new Future(() async {
      try {
        await sm.acquire();
        running.add(i);
        if (simultaneous < running.length) {
          simultaneous = running.length;
        }

        print("Start $i, running $running");
        await _doWork(100);
        running.remove(i);
        print("End   $i, running $running");
      } finally {
        sm.release();
      }
    }));
  }

  await Future.wait(tasks);
  print("Max permits: $maxCount, max simultaneous runned: $simultaneous");
}

Future _doWork(int ms) {
  // Simulate work
  return new Future.delayed(new Duration(milliseconds: ms));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  semaphore: ^0.1.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:semaphore/semaphore.dart';
  
Version Uploaded Documentation Archive
0.1.1 Feb 7, 2019 Go to the documentation of semaphore 0.1.1 Download semaphore 0.1.1 archive
0.1.0 Sep 14, 2018 Go to the documentation of semaphore 0.1.0 Download semaphore 0.1.0 archive
0.0.1 Oct 10, 2015 Go to the documentation of semaphore 0.0.1 Download semaphore 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
77
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
88
Learn more about scoring.

We analyzed this package on Mar 6, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:semaphore/semaphore.dart.

Health suggestions

Fix lib/src/semaphore/semaphore.dart. (-1.99 points)

Analysis of lib/src/semaphore/semaphore.dart reported 4 hints:

line 3 col 1: Prefer using /// for doc comments.

line 23 col 1: Prefer using /// for doc comments.

line 50 col 3: Prefer using /// for doc comments.

line 66 col 3: Prefer using /// for doc comments.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test ^1.5.3