8000 Guide: ensure barrier is in scope (#253) · socketry/async@c1f9d32 · GitHub
[go: up one dir, main page]

Skip to content

Commit c1f9d32

Browse files
authored
Guide: ensure barrier is in scope (#253)
This change avoids a NoMethodError on the barrier local variable. And also a LoadError, with the Async::Barrier constant.
1 parent dbc2f71 commit c1f9d32

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

guides/best-practices/readme.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ A barrier ensures you don't leak tasks and that all tasks are completed or stopp
3232

3333
```ruby
3434
require 'async'
35+
require 'async/barrier'
3536

3637
class Packages
3738
def initialize(urls)
3839
@urls = urls
3940
end
4041

4142
def fetch
42-
Sync do
43-
barrier = Async::Barrier.new
43+
barrier = Async::Barrier.new
4444

45+
Sync do
4546
@urls.map do |url|
4647
barrier.async do
4748
fetch(url)
@@ -60,6 +61,7 @@ Unbounded concurrency is a common source of bugs. Use a semaphore to limit the n
6061

6162
```ruby
6263
require 'async'
64+
require 'async/barrier'
6365
require 'async/semaphore'
6466

6567
class Packages
@@ -68,8 +70,9 @@ class Packages
6870
end
6971

7072
def fetch
73+
barrier = Async::Barrier.new
74+
7175
Sync do
72-
barrier = Async::Barrier.new
7376
# Only 10 tasks are created at a time:
7477
semaphore = Async::Semaphore.new(10, parent: barrier)
7578

0 commit comments

Comments
 (0)
0