A simple way to get multiplexing; run commands concurrently, combine their output streams.
Typically you feed it an array of items; Plywood yields the items one by one and expects you to make a command using that thing.
It then runs all the commands at the same time.
require "plywood"
items = %w[foo bar baz]
# There is no need to do this concurrently but this is just an example
Plywood::Commands.map(items) do |item|
"touch #{item}.txt" # the command that should be run
end
Plywood, by default, will use item.to_s
to use as the "process identifier". This identifier must be unique; and you are responsible for that. Often this is not a problem at all, say when you loop over a bunch of hostnames or chemical elements, but when you loop over a group of people it's very well possible that two of them share the same name.
To help with that you can specify the id
parameter. If it's a Symbol
Plywood will assume it's the name of a method on the item with arity 0 and call it. The return value is used as the process identifier. If it's a Proc
; Plywood will call it with the item as the only argument.
my_favorite_things = [
Thing.new("raindrops on roses"),
Thing.new("whiskers on kittens"),
Thing.new("bright copper kettles"),
Thing.new("warm woolen mittens"),
Thing.new("brown paper packages tied up with strings")]
Plywood::Commands.map(my_favorite_things, id: :object_id) do |thing|
"touch #{thing.underscore}.txt"
end
You can disable concurrent execution by using sequential: true
:
Plywood::Commands.map(my_favorite_things, sequential: true) do |thing|
"touch #{thing.underscore}.txt"
end