How to know whether current Fiber is the root Fiber?

Discussion in 'Ruby' started by Iñaki Baz Castillo, Apr 8, 2011.

  1. Hi, calling Fiber.yield without being in the context of a created
    Fiber raises an error:

    can't yield from root fiber (FiberError)

    It makes sense, of course. However I would like to know how to check
    whether I'm into the root Fiber or not. The documentation doesn't
    provide a method for this purpose. Is there a way? I expected
    something like Fiber#root?.

    Thanks a lot.


    --=20
    I=C3=B1aki Baz Castillo
    <>
     
    Iñaki Baz Castillo, Apr 8, 2011
    #1
    1. Advertising

  2. Iñaki Baz Castillo

    7stud -- Guest

    How about catching the exception?

    begin
    Fiber.yield
    rescue FiberError
    puts "In root fiber..."
    puts "... so I am going to do something different here."
    end

    puts 'executing rest of program'

    --output:--
    In root fiber...
    ... so I am going to do something different here.
    executing rest of program


    Or, maybe do something like this:

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Apr 8, 2011
    #2
    1. Advertising

  3. Iñaki Baz Castillo

    7stud -- Guest

    7stud -- wrote in post #991816:
    >
    > For example:
    >
    > require 'fiber'
    >
    > root_fiber = Fiber.current
    >
    > f = Fiber.new do
    > if Fiber.current.eql?(f)
    > puts 'not root fiber'
    > else
    > puts 'root fiber'
    > end
    >
    > Fiber.yield "hello world"
    > end
    >
    > f.resume
    >
    > --output:--
    > not root fiber
    > root fiber


    Instead, make that:

    require 'fiber'

    root_fiber = Fiber.current

    f = Fiber.new do
    if Fiber.current.eql?(root_fiber)
    puts 'root fiber'
    else
    puts 'not root fiber'
    end

    Fiber.yield "hello world"
    end

    f.resume

    if Fiber.current.eql?(root_fiber)
    puts 'root fiber'
    else
    puts 'not root fiber'
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Apr 8, 2011
    #3
  4. 2011/4/8 7stud -- <>:
    > Instead, make that:
    >
    > require 'fiber'
    >
    > root_fiber =3D Fiber.current
    >
    > f =3D Fiber.new do
    > =C2=A0if Fiber.current.eql?(root_fiber)
    > =C2=A0 =C2=A0puts 'root fiber'
    > =C2=A0else
    > =C2=A0 =C2=A0puts 'not root fiber'
    > =C2=A0end
    >
    > =C2=A0Fiber.yield "hello world"
    > end
    >
    > f.resume
    >
    > if Fiber.current.eql?(root_fiber)
    > =C2=A0puts 'root fiber'
    > else
    > =C2=A0puts 'not root fiber'
    > end


    Great solution :)

    Thanks a lot.

    --=20
    I=C3=B1aki Baz Castillo
    <>
     
    Iñaki Baz Castillo, Apr 11, 2011
    #4
  5. On Mon, Apr 11, 2011 at 9:30 AM, I=F1aki Baz Castillo <> wrote=
    :
    > 2011/4/8 7stud -- <>:
    >> Instead, make that:
    >>
    >> require 'fiber'
    >>
    >> root_fiber =3D Fiber.current
    >>
    >> f =3D Fiber.new do
    >> =A0if Fiber.current.eql?(root_fiber)
    >> =A0 =A0puts 'root fiber'
    >> =A0else
    >> =A0 =A0puts 'not root fiber'
    >> =A0end
    >>
    >> =A0Fiber.yield "hello world"
    >> end
    >>
    >> f.resume
    >>
    >> if Fiber.current.eql?(root_fiber)
    >> =A0puts 'root fiber'
    >> else
    >> =A0puts 'not root fiber'
    >> end

    >
    > Great solution :)


    You could even use #equal? instead of #eql? just in case #eql? is overridde=
    n.

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Apr 11, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Akihiro KAYAMA

    fiber(cooperative multi-threading)

    Akihiro KAYAMA, Dec 22, 2007, in forum: Python
    Replies:
    17
    Views:
    649
    Bjoern Schliessmann
    Dec 28, 2007
  2. Replies:
    0
    Views:
    277
  3. Suraj Kurapati

    Ruby 1.9 - equivalent of Fiber.current ?

    Suraj Kurapati, Mar 2, 2008, in forum: Ruby
    Replies:
    2
    Views:
    138
    Suraj Kurapati
    Mar 2, 2008
  4. Roger Pack
    Replies:
    3
    Views:
    166
    Roger Pack
    Jun 16, 2008
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    279
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page