Class: Spawn::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/spawn/task.rb

Constant Summary collapse

@@resources =

socket to close in child process

[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Task

Spawns a long-running section of code and returns the ID of the spawned process.



21
22
23
# File 'lib/spawn/task.rb', line 21

def initialize(options={})
  self.handle = fork_it(options) { yield }
end

Instance Attribute Details

#handleObject

Returns the value of attribute handle.



3
4
5
# File 'lib/spawn/task.rb', line 3

def handle
  @handle
end

Class Method Details

.close_resourcesObject

close all the resources added by calls to resource_to_close



14
15
16
17
18
# File 'lib/spawn/task.rb', line 14

def self.close_resources
  @@resources.each do |resource|
    resource.close if resource && resource.respond_to?(:close) && !resource.closed?
  end
end

.resource_to_close(resource) ⇒ Object

set the resource to disconnect from in the child process (when forking)



9
10
11
# File 'lib/spawn/task.rb', line 9

def self.resource_to_close(resource)
  @@resources << resource
end

Instance Method Details

#wait(sids = []) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/spawn/task.rb', line 25

def wait(sids = [])
  # wait for all threads and/or forks (if a single sid passed in, convert to array first)
  Array(sids).each do |sid|
    if sid.type == :thread
      sid.handle.join()
    else
      begin
        Process.wait(sid.handle)
      rescue
        # if the process is already done, ignore the error
      end
    end
  end
end