Home > Mobile >  ruby has nothing like a Fiber.kill?
ruby has nothing like a Fiber.kill?

Time:09-16

I'm using Fibers to handle scheduling between different chunks of code, and this has proven to be massively simpler than trying to manage Threads since my use case is fairly simple (and involves only running one script at a time). Unfortunately I need the ability to occasionally "kill" a Fiber, but it seems there is nothing in Fiber that is analogous to Thread.kill

My Fibers are just running a user supplied template, so it would be overly unwieldy to put in numerous "have I been killed and if so I need to return all the way to the top level". And also 'exit' in a Fiber just calls exit on the entire ruby interpreter.

Is there some way to either kill a fiber or have a fiber completely 'exit' from it's work?

Failing that (and perhaps this is another question, but it demonstrates why I'm looking for Fiber.kill) - I'm also looking into using Threads to basically implement Fibers (with the additional "kill" ability), but I don't see any way to do a thr.run or a thr.join that will wait until the thread has hit Thread.stop, and then allow me to take control again in my main scheduler, so Threads don't seem to be able to solve my situation either - short of having a main thread that just starts a thread and sits and polls for the thread to have finished it's segment of work, which seems messy and troubling at best.

CodePudding user response:

I'm struggling to understand how yield/resume isn't able to solve your problem - but when the fibre has yielded you can abort it using raise Fiber#raise


def build_fibre
  Fiber.new do |first|
    puts 'doing work'
    3.times { Fiber.yield false }

    puts 'finished work'
    true
  rescue
    puts 'aborted'
  end
end


fiber1 = build_fibre
fiber1.resume

# Raise an exception at the `yield` call
fiber1.raise

fiber2 = build_fibre
loop do
  puts 'waiting'
  break if fiber2.resume
end

puts 'finished'
  • Related