socketry / async

An awesome asynchronous event-driven reactor for Ruby.
MIT License
2.09k stars 86 forks source link

Add a concurrency primitive for waiting for a specific number of tasks to complete. #189

Closed ioquatix closed 1 year ago

ioquatix commented 1 year ago

See https://github.com/socketry/async/pull/174 for a slightly different implementation.

@bruno- @zhulik what do you think of this? You can compose it with barrier if you need a final wait-all, which simplifies the implementation and also makes it composable with semaphore, etc.

ioquatix commented 1 year ago

I'm not sure if the name of this is class is correct. Async::Waiter just seems too generic.

If you only care about waiting for some subset of tasks, this + a semaphore is probably more useful than a barrier. I think they serve different purposes.

bruno- commented 1 year ago

I like this implementation better 👍

Async::Waiter just seems too generic

Yes, and also the first association for waiter is that is should serve something

waiter

Maybe Async::Moat?

ioquatix commented 1 year ago
ioquatix commented 1 year ago

For deadline scheduling, it could also be useful:

barrier = Async::Barrier.new
waiter = Async::Waiter.new(parent: barrier)
urls.each{|url| waiter.async{... url ...}}
waiter.each(timeout: 5) do
  # Process as many completions as possible within 5 seconds
end
barrier.stop # kill all remaining tasks
bruno- commented 1 year ago

Async::LimitedBarrier sounds the best IMO.

zhulik commented 1 year ago

I like this implementation and Async::LimitedBarrier seems to be a good name IMO