fiber = spin do
do_some_work
end
fiber = spin_loop do
iterate_on_something
end
fiber.stop
# or:
fiber.interrupt
fiber.await
# or:
fiber.join
Fiber.await(fiber1, fiber2, ...)
# or:
Fiber.join(fiber1, fiber2, ...)
fiber.raise(SomeException)
# or:
fiber.raise(SomeException, 'Exception message')
fiber.restart
# or:
fiber.reset
def calculate_some_stuff(n)
acc = 0
n.times do |i|
acc += big_calc(acc, i)
snooze if (i % 1000) == 0
end
end
suspend
fiber.schedule
# or:
fiber.schedule(some_value)
fiber << message
# or:
fiber.send << message
message = receive
# or, using deconstructing assign
a, b, c = receive
sleep 1 # sleeps for 1 second
sleep
# or:
suspend
every(10) { do_something } # perform an operation once every 10 seconds
throttled_loop(10) { do_something } # perform an operation 10 times per second
# On timeout, a Polyphony::Cancel exception is raised
cancel_after(10) { do_something_slow } # timeout after 10 seconds
# Or, using the stock Timeout API, raising a Timeout::Error
Timeout.timeout(10) { do_something_slow }
# On timeout, result will be set to nil
result = move_on_after(10) { do_something_slow }
# Or, with a specific value:
result = move_on_after(10, with_value: 'foo') { do_something_slow }
# works with move_on_after as well
cancel_after(10) do |timeout|
while (data = client.gets)
timeout&.reset
do_something_with_data(client)
end
end
# At least one file descriptor should be a pipe
dest.splice_from(source, 8192) #=> returns number of bytes spliced
# or:
IO.splice(src, dest, 8192)
dest.splice_from(source, -8192) #=> returns number of bytes spliced
# or:
IO.splice(src, dest, -8192)
dest2.tee_from(source, 8192)
dest1.splice_from(source, 8192)
# or:
IO.tee(src, dest2)
IO.splice(src, dest1)
# This will automatically create a pipe, and splice data from source to
# destination until EOF is encountered.
IO.double_splice(src, dest)
A Polyphony::Pipe
instance encapsulates a pipe with two file descriptors. It
can be used just like any other IO instance for reading, writing, splicing etc.
pipe = Polyphony::Pipe.new
# or:
pipe = Polyphony.pipe
IO.gzip(src, dest)
IO.gunzip(src, dest)
IO.deflate(src, dest)
IO.inflate(src, dest)
server_socket.accept_loop do |conn|
handle_connection(conn)
end
connection.read_loop do |data|
handle_data(data)
end
unpacker = MessagePack::Unpacker.new
reader = spin do
io.feed_loop(unpacker, :feed_each) { |msg| handle_msg(msg) }
end