Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby EventMachine, kill running processes?

How can I kill running processes in EventMachine? Below is an example, I'm starting 10 processes and then I'm trying to erase them all (but it doesn't work). My goal is to not have the "Finished" output.

require "rubygems"
require "eventmachine"

class Event
  def start
    sleep(5)
    puts Time.now.to_s + ": Finished!"
  end
end

EventMachine.run do
  events = []
  10.times {
    handle = Event.new
    events << handle

    EventMachine.defer(proc {
      handle.start
    })
  }

  # Terminate all events!
  events.each do |handle|
    handle = nil
    ObjectSpace.garbage_collect
  end
end

I'm aware that I could set a variable and check whether it's set when doing the output, but I feel like this isn't the "real" thing, or is this really the only solution there is?

like image 630
Benedikt B Avatar asked Dec 19 '25 21:12

Benedikt B


1 Answers

Try EventMachine.stop_event_loop, it will “cause all open connections and accepting servers to be run down and closed”.

like image 55
Patrick Oscity Avatar answered Dec 21 '25 19:12

Patrick Oscity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!