10000 Add guides about barriers with a fan out and termination by trevorturk · Pull Request #205 · socketry/async · GitHub
[go: up one dir, main page]

Skip to content

Add guides about barriers with a fan out and termination #205

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

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add guides about barriers with a fan out and termination
  • Loading branch information
trevorturk committed Dec 5, 2022
commit 0df552cc9e9827966872e65b59ad076293550c8b
40 changes: 40 additions & 0 deletions guides/asynchronous-tasks/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,46 @@ Async do
end
```

### Stopping all Tasks held in a Barrier

To stop (terminate/cancel) the all tasks held in a barrier:

```ruby
barrier = Async::Barrier.new

Async do
tasks = 3.times.map do |i|
barrier.async do
sleep 1
puts "Hello World #{i}"
end
end

barrier.stop
end
```

If you're letting individual tasks held by a barrier throw unhandled exceptions, be sure to call ({ruby Async::Barrier#stop}):

```ruby
barrier = Async::Barrier.new

Async do
tasks = 3.times.map do |i|
barrier.async do
sleep 1
puts "Hello World #{i}"
end
end

begin
barrier.wait
ensure
barrier.stop
end
end
```

## Resource Management

In order to ensure your resources are cleaned up correctly, make sure you wrap resources appropriately, e.g.:
Expand Down
0