Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ Ost[:rss_feeds].each do |id|
end
```

Each with concurrency

``` ruby
require "ost"

Ost[:rss_feeds].each_with_concurrency(3) do |id| #default 3
puts "*** #{id} | th: #{Thread.current.object_id}"
end
```

It will pop items from the queue as soon as they become available. It
uses `BRPOPLPUSH` with a timeout that can be specified with the
`OST_TIMEOUT` environment variable.
Expand Down
9 changes: 9 additions & 0 deletions lib/ost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def each(&block)
end
end

def each_with_concurrency(size = 3, &block)
concurrency = Array.new(size).map do |iteration|
Thread.new(iteration) do |iteration|
each(&block)
end
end
concurrency.each(&:join)
end

def stop
@stopping = true
end
Expand Down