Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prathmeshranaut/18375ebe1b04b859fb27b906ea1e4a0c to your computer and use it in GitHub Desktop.
Save prathmeshranaut/18375ebe1b04b859fb27b906ea1e4a0c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
$LOAD_PATH.push File.expand_path("../../lib", __FILE__)
require "celluloid/autostart"
require "mathn"
#
# Basic spawn example
#
class PrimeWorker
include Celluloid
def prime(number)
puts "slept #{number}"
return 1 if number.prime?
end
end
# Spawn of PrimeWorker
worker = PrimeWorker.spawn
(1..5).to_a.map do |i|
# Call the prime function asynchronously on the spawned threads
worker.async.prime i
puts "Main Thread : #{i}"
end
# OUTPUT
# I, [2016-08-07T21:26:17.438360 #86823] INFO -- : Celluloid 0.17.5 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
# Main Thread : 1
# Main Thread : 2
# Main Thread : 3
# Main Thread : 4
# slept 1
# Main Thread : 5
# slept 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment