Class: Concurrently::EventLoop
- Inherits:
-
Object
- Object
- Concurrently::EventLoop
- Defined in:
- lib/all/concurrently/event_loop.rb
Overview
Although you probably won't need to interact with the event loop
directly (unless you call Kernel#fork
, see #reinitialize!), you need
to understand that it's there.
Event loops are not thread safe. But since each thread has its own event loop they are not shared anyway.
Concurrently::EventLoop
, like any event loop, is the heart of your
application and must never be interrupted, blocked or overloaded. A
healthy event loop is one that can respond to new events immediately.
The loop runs in the background and you won't interact with it directly.
Instead, when you call #wait
or one of the #await_*
methods the
bookkeeping of selecting IOs for readiness or waiting a given amount of
time is done for you.
Class Method Summary collapse
-
.current ⇒ Object
The event loop of the current thread.
Instance Method Summary collapse
-
#lifetime ⇒ Object
The lifetime of this event loop in seconds.
-
#reinitialize! ⇒ Object
Resets the inner state of the event loop.
Class Method Details
.current ⇒ Object
The event loop of the current thread.
This method is thread safe. Each thread returns its own event loop.
27 28 29 |
# File 'lib/all/concurrently/event_loop.rb', line 27 def self.current @current ||= new end |
Instance Method Details
#lifetime ⇒ Object
The lifetime of this event loop in seconds
97 98 99 |
# File 'lib/all/concurrently/event_loop.rb', line 97 def lifetime Time.now.to_f - @start_time end |
#reinitialize! ⇒ Object
The exclamation mark in its name stands for: Watch out! This method will break stuff if not used in the right place.
Resets the inner state of the event loop.
In detail, calling this method for the event loop:
- resets its #lifetime,
- clears its internal run queue,
- clears its internal list of watched IOs,
- clears its internal pool of fibers.
While this method clears the list of IOs watched for readiness, the IOs themselves are left untouched. You are responsible for managing IOs (e.g. closing them).
64 65 66 67 68 69 70 71 |
# File 'lib/all/concurrently/event_loop.rb', line 64 def reinitialize! @start_time = Time.now.to_f @run_queue = RunQueue.new self @io_selector = IOSelector.new self @proc_fiber_pool = ProcFiberPool.new self @fiber = Fiber.new @run_queue, @io_selector, @proc_fiber_pool self end |