RSS Amplifier

Ben Wendt's blog · May 10, 2017

Popping errors out from ruby threads using a Queue

0
Sign in to vote or save

This page cannot be shown here. You can still read it on the original site — the toolbar below keeps your place in the directory.

I had an issue at work where some database writes were throwing errors within a thread, and the error was being silently eaten when the thread died. The solution is to use a queue for inter-thread communication, like this: q = Queue.new Thread.new do i = 1 loop do begin sleep i i += 1 raise 'whatever' rescue StandardError => e q << e end end end loop do puts q.size end

I had an issue at work where some database writes were throwing errors within a thread, and the error was being silently eaten when the thread died.

The solution is to use a queue for inter-thread communication, like this:

q = Queue.new
Thread.new do
    i = 1
    loop do
    begin
        sleep i
        i += 1
        raise 'whatever'
    rescue StandardError => e
        q << e
    end
    end
end
loop do
    puts q.size
end

Read on localhost

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.