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

Streamlining the WaitGroup API #14820

Closed
jgaskins opened this issue Jul 21, 2024 · 4 comments · Fixed by #14837
Closed

Streamlining the WaitGroup API #14820

jgaskins opened this issue Jul 21, 2024 · 4 comments · Fixed by #14837

Comments

@jgaskins
Copy link
Contributor

Feature Request

  • Is your feature request related to a problem? Please describe clearly and concisely what is it.

Less of a problem, more of an itch I'd like to scratch. WaitGroup is something I've been wanting in Crystal for years, but the API is a bit Golang-like.

wg = WaitGroup.new(2)
spawn do
  http.get "/foo"
ensure
  wg.done
end
spawn do
  http.get "/bar"
ensure
  wg.done
end
wg.wait
  • Describe the feature you would like, optionally illustrated by examples, and how it will solve the above problem.

I've been experimenting with an API that looks like this and it works really nicely so far.

WaitGroup.wait do |wg|
  wg.spawn { http.get "/foo" }
  wg.spawn { http.get "/bar" }
end

It's got the same feel as Redis pipelines:

redis.pipeline do |pipe|
  pipe.get "foo"
  pipe.get "bar"
end
  • Describe considered alternative solutions, and the reasons why you have not proposed them as a solution here.

I don't have any alternatives at the moment, but it's perfectly understandable if the method names change.

  • Does it break backward compatibility, if yes then what's the migration path?

Nope, the idea builds on top of the existing WaitGroup API.

@stakach
Copy link
Contributor

stakach commented Jul 22, 2024

similar to my promise library too
https://github.com/spider-gazelle/promise?tab=readme-ov-file#promiseall

I'm all for this, much harder to shoot yourself in the foot and looks way better

@ysbaddaden
Copy link
Contributor

ysbaddaden commented Jul 22, 2024

RFC #3 had a #spawn(&) method but it looks it didn't make it to the pull request.

The .wait(&) method looks like a nice addition.

@straight-shoota
Copy link
Member

WaitGroup is a low-level primitive and IMO shouldn't need to be super polished for convenience. The idea is that most people wouldn't use it directly, in favour of a higher level concurrency API (cf. #6468). At the moment such a higher level API is still missing. But once we complete that, I suppose there would be little reason to use WaitGroup directly.

@bararchy
Copy link
Contributor

I think spawn makes sense even for a low level API?
btw, would WaitGroup work for something like

select
when bail.receive
when wg.wait
end

That would allow to make it a basis in a worker-pool implementation, or maybe even have some wg.release mechanism for when you want multiple workers to do something but first one done means we can release the others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants