Last active
December 24, 2015 16:09
-
-
Save kgrz/6826255 to your computer and use it in GitHub Desktop.
Possible answer to SO question: http://stackoverflow.com/questions/19172707/ruby-event-machine-stop-or-kill-deffered-operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'em-websocket' | |
EM.run do | |
EM::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |ws| | |
q = EM::Queue.new | |
l = Mutex.new | |
ws.onmessage do |msg| | |
op = proc do | |
l.synchronize do | |
q.push Thread.current | |
q.size.times do | |
q.pop { |t| t.kill } | |
end | |
end | |
puts "Received #{msg}. will now do a looooong process" | |
sleep 10 # Thread safe IO here that is safely killed | |
true | |
end | |
callback = proc do |result| | |
puts "Done! with #{msg}" | |
end | |
EM.defer(op, callback) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment