Async::Container SourceAsyncContainer

module Container

Nested

Definitions

def self.fork?

Whether the underlying process supports fork.

Signature

returns Boolean

Implementation

def self.fork?
	::Process.respond_to?(:fork) && ::Process.respond_to?(:setpgid)
end

def self.best_container_class

Determins the best container class based on the underlying Ruby implementation. Some platforms, including JRuby, don't support fork. Applications which just want a reasonable default can use this method.

Signature

returns Class

Implementation

def self.best_container_class
	if fork?
		return Forked
	else
		return Threaded
	end
end

def self.new(*arguments, **options)

Create an instance of the best container class.

Signature

returns Generic

Typically an instance of either class Async::Container::Forked or class Async::Container::Threaded containers.

Implementation

def self.new(*arguments, **options)
	best_container_class.new(*arguments, **options)
end

ASYNC_CONTAINER_PROCESSOR_COUNT = 'ASYNC_CONTAINER_PROCESSOR_COUNT'

An environment variable key to override .processor_count.

def self.processor_count(env = ENV)

The processor count which may be used for the default number of container threads/processes. You can override the value provided by the system by specifying the ASYNC_CONTAINER_PROCESSOR_COUNT environment variable.

Signature

returns Integer

The number of hardware processors which can run threads/processes simultaneously.

raises RuntimeError

If the process count is invalid.

Implementation

def self.processor_count(env = ENV)
	count = env.fetch(ASYNC_CONTAINER_PROCESSOR_COUNT) do
		Etc.nprocessors rescue 1
	end.to_i
	
	if count < 1
		raise RuntimeError, "Invalid processor count #{count}!"
	end
	
	return count
end