Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Add getter for _allocatedResources private field #33

Open
HelgeSverre opened this issue Sep 8, 2019 · 0 comments · May be fixed by #70
Open

[Feature Request] Add getter for _allocatedResources private field #33

HelgeSverre opened this issue Sep 8, 2019 · 0 comments · May be fixed by #70

Comments

@HelgeSverre
Copy link

HelgeSverre commented Sep 8, 2019

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?

@aeb-dev aeb-dev linked a pull request May 29, 2023 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant