Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
ruby has nothing like a Fiber.kill?

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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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'
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda