class Fiber
- Fiber
- Reference
- Object
Overview
A Fiber
is a light-weight execution unit managed by the Crystal runtime.
It is conceptually similar to an operating system thread but with less overhead and completely internal to the Crystal process. The runtime includes a scheduler which schedules execution of fibers.
A Fiber
has a stack size of 8 MiB
which is usually also assigned
to an operating system thread. But only 4KiB
are actually allocated at first
so the memory footprint is very small.
Communication between fibers is usually passed through Channel
.
Cooperative
Fibers are cooperative. That means execution can only be drawn from a fiber when it offers it. It can't be interrupted in its execution at random. In order to make concurrency work, fibers must make sure to occasionally provide hooks for the scheduler to swap in other fibers. IO operations like reading from a file descriptor are natural implementations for this and the developer does not need to take further action on that. When IO access can't be served immediately by a buffer, the fiber will automatically wait and yield execution. When IO is ready it's going to be resumed through the event loop.
When a computation-intensive task has none or only rare IO operations, a fiber
should explicitly offer to yield execution from time to time using
Fiber.yield
to break up tight loops. The frequency of this call depends on
the application and concurrency model.
Event loop
The event loop is responsible for keeping track of sleeping fibers waiting for notifications that IO is ready or a timeout reached. When a fiber can be woken, the event loop enqueues it in the scheduler
Defined in:
context.crInstance Method Summary
-
#[](key : Symbol) : Value
fetch a context value using the passed symbol as key, if the key is not known an
KeyError
will be raised. -
#[]?(key : Symbol) : Log::Metadata::Value | Nil
fetch a context value using the passed symbol as key, if the key is not known
nil
will be returned -
#each_value(&block : Tuple(Symbol, Log::Metadata::Value) -> )
iterates over all context values
-
#fetch_value(key, &block)
fetch a context value using the passed symbol as key, if the key is not known the context will be executed and its result will be returned
-
#inspect_values(io : IO) : Nil
inspects all values and writes them to the given
io
-
#inspect_values : String
inspects all values and returns them as a string
-
#with_empty_values(**kwargs, &)
adds the passed key-value pairs to the current fiber context without inherinting the previous context values.
-
#with_values(**kwargs, &)
adds the passed key-value pairs to the current fiber context.
Instance Method Detail
fetch a context value using the passed symbol as key, if the key is not
known an KeyError
will be raised.
fetch a context value using the passed symbol as key, if the
key is not known nil
will be returned
fetch a context value using the passed symbol as key, if the key is not known the context will be executed and its result will be returned
adds the passed key-value pairs to the current fiber context without
inherinting the previous context values.
The values are only accessible in the context of the passed block.
The values can be shadowed by calling #with_values
nested.
adds the passed key-value pairs to the current fiber context.
The values are only accessible in the context of the passed block.
The values can be shadowed by calling #with_values
nested.