Module: Wrest::AsyncRequest

Defined in:
lib/wrest/async_request.rb,
lib/wrest/async_request/thread_pool.rb,
lib/wrest/async_request/thread_backend.rb,
lib/wrest/async_request/event_machine_backend.rb

Defined Under Namespace

Classes: EventMachineBackend, ThreadBackend, ThreadPool

Class Method Summary collapse

Class Method Details

.default_backendObject

Returns the default backend, which is the ThreadBackend



35
36
37
# File 'lib/wrest/async_request.rb', line 35

def self.default_backend
  @default_backend || default_to_threads!
end

.default_backend=(backend) ⇒ Object

Assign default backend to be used for asynchronous request. Default is to use threads



19
20
21
# File 'lib/wrest/async_request.rb', line 19

def self.default_backend=(backend)
  @default_backend = backend
end

.default_to_em!Object

Assign default backend for asynchronous request to using eventmachine.



24
25
26
27
# File 'lib/wrest/async_request.rb', line 24

def self.default_to_em!
  self.enable_em
  self.default_backend = Wrest::AsyncRequest::EventMachineBackend.new
end

.default_to_threads!(number_of_threads = 5) ⇒ Object

Assign default backend for asynchronous request to using threads.



30
31
32
# File 'lib/wrest/async_request.rb', line 30

def self.default_to_threads!(number_of_threads = 5)
  self.default_backend = Wrest::AsyncRequest::ThreadBackend.new(number_of_threads)
end

.enable_emObject

Loads Wrest eventmachine backend alongwith eventmachine gem



14
15
16
# File 'lib/wrest/async_request.rb', line 14

def self.enable_em
  require "wrest/async_request/event_machine_backend"
end

.wait_for_thread_pool!Object

Uses Thread#join to wait until all background requests are completed.

Use this as the last instruction in a script to prevent it from exiting before background threads have completed running.

Needs Wrest.default_backend to be an instance of ThreadBackend.



46
47
48
# File 'lib/wrest/async_request.rb', line 46

def self.wait_for_thread_pool!
  default_backend.wait_for_thread_pool!
end