lib/async/container/threaded.rb in async-container-0.10.2 vs lib/async/container/threaded.rb in async-container-0.11.0
- old
+ new
@@ -18,16 +18,18 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'async/reactor'
require 'thread'
+
+require_relative 'controller'
require_relative 'statistics'
module Async
module Container
# Manages a reactor within one or more threads.
- class Threaded
+ class Threaded < Controller
class Instance
def initialize(thread)
@thread = thread
end
@@ -38,26 +40,22 @@
def self.run(*args, &block)
self.new.run(*args, &block)
end
+ def self.multiprocess?
+ false
+ end
+
def initialize
@threads = []
@running = true
@statistics = Statistics.new
end
attr :statistics
- def run(count: Container.processor_count, **options, &block)
- count.times do
- async(**options, &block)
- end
-
- return self
- end
-
def spawn(name: nil, restart: false, &block)
@statistics.spawn!
thread = ::Thread.new do
thread = ::Thread.current
@@ -88,47 +86,29 @@
@threads << thread
return self
end
- def async(name: nil, restart: false, &block)
- spawn do |instance|
- begin
- Async::Reactor.run(instance, &block)
- rescue Interrupt
- # Graceful exit.
- end
- end
-
- return self
- end
-
- def self.multiprocess?
- false
- end
-
def wait
- yield if block_given?
-
@threads.each(&:join)
@threads.clear
return nil
rescue Interrupt
# Graceful exit.
end
# Gracefully shut down all reactors.
- def stop(graceful = true, &block)
+ def stop(graceful = true)
@running = false
if graceful
@threads.each{|thread| thread.raise(Interrupt)}
else
@threads.each(&:kill)
end
- self.wait(&block)
+ self.wait
ensure
@running = true
end
end
end