Class: Thespian::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/thespian/example.rb

Overview

This class aids in running the examples in the different modes.

To run in threaded mode, just run without arguments:

bundle exec ruby examples/task_processor.rb

To run in fibered mode:

bundle exec ruby examples/task_processor.rb --fiber

This class does all the necessary incantations necessary to run in fibered mode (starts/stops EventMachine, wraps in root fiber, etc).

Class Method Summary collapse

Class Method Details

.run(&example) ⇒ Object

Determine what mode to run in by looking at ARGV then invoke the block given. If running in fibered mode, the block will be wrapped with necessary calls to EventMachine and Fiber. It will also make sure to define fiber safe versions of #sleep and #pass that the example can use.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/thespian/example.rb', line 16

def self.run(&example)
  if ARGV.include?("--fiber") || ARGV.include?("--fibers")
    require "eventmachine"
    require "strand"
    EM.run do
      Strand.new do
        puts "Running example with fibers..."
        Thespian::Actor::DEFAULT_OPTIONS[:mode] = :fiber
        example.binding.eval "          def sleep(n); Strand.sleep(n); end\n          def pass; Strand.pass; end\n        CODE\n        example.call\n        EM.stop\n      end\n    end\n  else\n    puts \"Running example with threads...\"\n    example.binding.eval <<-CODE\n      def pass; Thread.pass; end\n    CODE\n    example.call\n  end\nend\n"