semaphore 0.1.0

  • README.md
  • Example
  • Installing
  • Versions
  • 80

semaphore

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

Version: 0.0.1

The goal of the Dash effort is ultimately to replace JavaScript as the lingua franca of web development on the open web platform.

Example:

import 'dart:async';

import 'package:semaphore/semaphore.dart';

Future main() async {
  var maxCount = 3;
  var running = 0;
  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++;
        if (simultaneous < running) {
          simultaneous = running;
        }

        print("Start $i, running $running");
        await _doWork(100);
        running--;
        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 1
Start 1, running 2
Start 2, running 3
End   0, running 2
Start 3, running 3
End   1, running 2
Start 4, running 3
End   2, running 2
Start 5, running 3
End   3, running 2
Start 6, running 3
End   4, running 2
Start 7, running 3
End   5, running 2
Start 8, running 3
End   6, running 2
End   7, running 1
End   8, running 0
Max permits: 3, max simultaneous runned: 3

example/example.dart

import 'dart:async';

import 'package:semaphore/semaphore.dart';

Future main() async {
  var maxCount = 3;
  var running = 0;
  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++;
        if (simultaneous < running) {
          simultaneous = running;
        }

        print("Start $i, running $running");
        await _doWork(100);
        running--;
        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.0

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.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]
69
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
80
Learn more about scoring.

We analyzed this package on Nov 14, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.6

Platforms

Detected platforms: Flutter, web, other

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

Maintenance issues and suggestions

Maintain CHANGELOG.md. (-20 points)

Changelog entries help clients to follow the progress in your code.

Dependencies

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