Fiber::ExecutionContext::Isolated
Inherits Fiber::ExecutionContext::Scheduler / Fiber::ExecutionContext / Reference / Object
Isolated execution context to run a single fiber.
Concurrency and parallelism are disabled. The context guarantees that the fiber will always run on the same system thread until it terminates; the fiber owns the system thread for its whole lifetime.
Keep in mind that the fiber will still run in parallel to other fibers running in other execution contexts at the same time.
Concurrency is disabled, so an isolated fiber can't spawn fibers into the
context, but it can spawn fibers into other execution contexts. Since it can
be inconvenient to pass an execution context around, calls to ::spawn will
spawn a fiber into the specified spawn_context during initialization,
which defaults to Fiber::ExecutionContext.default.
Isolated fibers can normally communicate with other fibers running in other
execution contexts using Channel, WaitGroup or Sync for example. They
can also execute IO operations or sleep just like any other fiber.
Calls that result in waiting (e.g. sleep, or socket read/write) will block the thread since there are no other fibers to switch to. This in turn allows to call anything that would block the thread without blocking any other fiber.
For example you can start an isolated fiber to run a blocking GUI loop,
transparently forward ::spawn to the default context, then keep the main
fiber to wait until the GUI application quit:
gtk = Fiber::ExecutionContext::Isolated.new("Gtk") do
Gtk.main
end
gtk.wait
Constructors
Instance methods
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
Instantiates a fiber and enqueues it into the scheduler's local queue.
Returns the current status of the scheduler. For example "running",
"event-loop" or "parked".
Appends a short String representation of this object which includes its class name and its object address.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).to_s # => #<Person:0x10a199f20>
Blocks the calling fiber until the isolated context fiber terminates. Returns immediately if the isolated fiber has already terminated. Re-raises unhandled exceptions raised by the fiber.
For example:
ctx = Fiber::ExecutionContext::Isolated.new("test") do
raise "fail"
end
ctx.wait # => re-raises "fail"