Class: Concurrently::EventLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/all/concurrently/event_loop.rb

Overview

Note:

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.

Note:

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.

Since:

  • 1.0.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currentObject

The event loop of the current thread.

This method is thread safe. Each thread returns its own event loop.

Examples:

Concurrently::EventLoop.current

Since:

  • 1.0.0


27
28
29
# File 'lib/all/concurrently/event_loop.rb', line 27

def self.current
  @current ||= new
end

Instance Method Details

#lifetimeObject

The lifetime of this event loop in seconds

Examples:

Concurrently::EventLoop.current.lifetime # => 2.3364

Since:

  • 1.0.0


97
98
99
# File 'lib/all/concurrently/event_loop.rb', line 97

def lifetime
  Time.now.to_f - @start_time
end

#reinitialize!Object

Note:

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).

Examples:

fork do
  Concurrently::EventLoop.current.reinitialize!
  # ...
end

# ...

Since:

  • 1.0.0


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