FiberError: 'fiber called across threads' using em-synchrony

417 Views Asked by At

I am using em-synchrony and em-http-request (Ruby 2.1) inside a sidekiq worker to scrape a number of links concurrently. Everything works well, except when I try and run multiple workers with the scraper at the same time. This is the code I'm using:

def process(web_pages)
  EM.synchrony do
    EM::Synchrony::Iterator.new(web_pages, concurrency=50).map do |web_page, iter|
      EventMachine::HttpRequest.new(web_page.url, request_options).get(redirects:10)
    end
    EventMachine.stop
  end
end

When this worker process is run with multiple instances running at the same time, it eventually throws the following error:

WARN: FiberError: fiber called across threads
WARN: ruby-2.1.0/gems/em-synchrony-1.0.4/lib/em-synchrony/iterator.rb:28:in `resume'
/ruby-2.1.0/gems/em-synchrony-1.0.4/lib/em-synchrony/iterator.rb:28:in `block in map'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:172:in `call'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:172:in `block in map'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:206:in `call'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:206:in `block in inject'
/ruby-2.1.0/gems/em-synchrony-1.0.4/lib/em-synchrony/iterator.rb:17:in `call'
/ruby-2.1.0/gems/em-synchrony-1.0.4/lib/em-synchrony/iterator.rb:17:in `block in each'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:95:in `call'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:95:in `block in each'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:117:in `call'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:117:in `block (2 levels) in each'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:195:in `call'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:195:in `block (2 levels) in inject'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:161:in `call'
/ruby-2.1.0/gems/eventmachine-1.0.8/lib/em/iterator.rb:161:in `block (2 levels) in map'
0

There are 0 best solutions below