pool icon indicating copy to clipboard operation
pool copied to clipboard

[Feature Request] Add getter for _allocatedResources private field

Open HelgeSverre opened this issue 6 years ago • 0 comments

This would be useful to check if the "worker pool" after a certain timeout is "finished" with all the tasks.

Example of use case:

import 'dart:async';
import 'dart:collection';
import 'dart:mirrors';

import 'package:async/async.dart';
import 'package:pool/pool.dart';

void main() async {
  Queue<String> queue = Queue();
  Pool workerPool = new Pool(2, timeout: Duration(seconds: 1));
  queue.addAll(["Hey", "there", "dart", "team"]);

  // If the loop is "inactive" for 5 seconds, processing should stop.
  var inactivityTimer = RestartableTimer(Duration(seconds: 5), () {});

  while (true) {
    if (!inactivityTimer.isActive) {
      // RELEVANT LINE: The queue is empty, the inactivity timer has been triggered, there is currently no way besides reflection to check if the pool resources has all completed 
      // Wait until all all work is done
      if (workerPool.allocatedResources == 0) {
        print("All workers are done");
        break;
      }
    }

    // The queue is empty, wait to check again
    if (queue.isEmpty) {
      await Future.delayed(Duration(seconds: 5));
    } else {
     // We have work to do, create a new pooled resource to work on it
      workerPool.withResource(() async {
        await Future.delayed(Duration(seconds: 10));
        print(queue.removeFirst())
      });
      inactivityTimer.reset();
    }

    // If we pummel the workerPool with resource requests too fast, it locks up.
    await Future.delayed(Duration(milliseconds: 10));
  }
}

(Code is condensed to the basic idea, based on a web crawler I am working on).

Is this a feature that you will consider adding to this library?

HelgeSverre avatar Sep 08 '19 18:09 HelgeSverre