akawhy
akawhy

Reputation: 1608

Why process blocked forever when call sleep in Celluloid::IO

I'm using Celluloid::IO to do DNS query and below is my code:

require 'celluloid/io'

class MyResolver
    include Celluloid::IO

    def initialize
        @resolver = DNSResolver.new
    end 

    def resolve(domain)
        ips = @resolver.resolve domain

        #sleep 1
        return {domain: domain, ip: ips}
    end 
end

pool = MyResolver.pool(size: 5)

domains = [ 
    'www.google.com',
    ## many other record
]

futures = domains.map {|d| pool.future.resolve(d)}

futures.each do |future|
    puts "#{future.value}"
end

This code works and finished in few seconds. But when I add the line sleep 1(just for learning purpose), after printing some results, the process blocked forever, which is very strange.

Thanks for any help.

Upvotes: 2

Views: 344

Answers (2)

digitalextremist
digitalextremist

Reputation: 5993

sleep is an overridden keyword in Celluloid, so if you want sleep from Ruby itself, use Kernel.sleep. But that being said, as of 0.17.0-dependent branch of Celluoid::IO this error you describe does not exist ( anymore? ).

I used your reproducible failing case to test the new celluloid-pool gem being released in version 0.17.0 of Celluloid, and it is working no problem with sleep 1 as is.

Upvotes: 1

Oleg Kr
Oleg Kr

Reputation: 469

Something is wrong with DNSResolver at least in that case, but you can use "thread-aware DNS resolver" Resolv from standard ruby library - no any blocks with ~25k domains array. Don't forget to catch exceptions from Resolv.

Upvotes: 0

Related Questions