Skip to content

[Question] How to stop a fiber? #263

@jsaak

Description

@jsaak

I have still no idea how to stop a Fiber from running (unschedule).
I was told that i can raise an exception, but it is not working as i hoped.
Can you help me how to do that?

In this code i was expecting @fib1 to exit, but it does not exit ever.

Do I have any alternative options?

require 'async'

Fiber.set_scheduler(Async::Scheduler.new)

@fib1 = Fiber.schedule do
  puts 'entering fiber 1'
  sleep 1
  @fib2.raise
  puts 'exiting fiber 1'
end

@fib2 = Fiber.schedule do
  puts 'entering fiber 2'
  begin
    sleep 4
  rescue
    puts 'got exception'
  end
  puts 'exiting fiber 2'
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions