Class: Fiber
- Inherits:
-
Object
- Object
- Fiber
- Defined in:
- lib/rage/fiber.rb
Class Method Summary collapse
-
.await(*fibers) ⇒ Object
Wait on several fibers at the same time.
Class Method Details
.await(*fibers) ⇒ Object
Note:
This method should only be used when multiple fibers have to be processed in parallel. There's no need to use Fiber.await
for single IO calls.
Wait on several fibers at the same time. Calling this method will automatically pause the current fiber, allowing the server to process other requests. Once all fibers have completed, the current fiber will be automatically resumed.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rage/fiber.rb', line 22 def self.await(*fibers) f = Fiber.current num_wait_for = fibers.count(&:alive?) return fibers.map(&:__get_result) if num_wait_for == 0 Iodine.subscribe("await:#{f.object_id}") do num_wait_for -= 1 f.resume if num_wait_for == 0 end Fiber.yield Iodine.defer { Iodine.unsubscribe("await:#{f.object_id}") } fibers.map(&:__get_result) end |