Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2525 > unrolled thread

How to know whether current Fiber is the root Fiber?

Started byIñaki Baz Castillo <ibc@aliax.net>
First post2011-04-08 07:30 -0500
Last post2011-04-11 02:52 -0500
Articles 5 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  How to know whether current Fiber is the root Fiber? Iñaki Baz Castillo <ibc@aliax.net> - 2011-04-08 07:30 -0500
    Re: How to know whether current Fiber is the root Fiber? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-08 13:37 -0500
      Re: How to know whether current Fiber is the root Fiber? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-08 13:56 -0500
        Re: How to know whether current Fiber is the root Fiber? Iñaki Baz Castillo <ibc@aliax.net> - 2011-04-11 02:30 -0500
          Re: How to know whether current Fiber is the root Fiber? Robert Klemme <shortcutter@googlemail.com> - 2011-04-11 02:52 -0500

#2525 — How to know whether current Fiber is the root Fiber?

FromIñaki Baz Castillo <ibc@aliax.net>
Date2011-04-08 07:30 -0500
SubjectHow to know whether current Fiber is the root Fiber?
Message-ID<BANLkTinQAU_BbiTucU5DKxNXiFXyTcE9TQ@mail.gmail.com>
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.


-- 
Iñaki Baz Castillo
<ibc@aliax.net>

[toc] | [next] | [standalone]


#2544

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-08 13:37 -0500
Message-ID<924f39251213390fb702027274090882@ruby-forum.com>
In reply to#2525
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/.

[toc] | [prev] | [next] | [standalone]


#2552

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-08 13:56 -0500
Message-ID<306caacb747dfc99c7d49c815ad456cb@ruby-forum.com>
In reply to#2544
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/.

[toc] | [prev] | [next] | [standalone]


#2611

FromIñaki Baz Castillo <ibc@aliax.net>
Date2011-04-11 02:30 -0500
Message-ID<BANLkTimTtajxQhG4W+1LmTwudQ4Kw140Bg@mail.gmail.com>
In reply to#2552
2011/4/8 7stud -- <bbxx789_05ss@yahoo.com>:
> 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

Great solution :)

Thanks a lot.

-- 
Iñaki Baz Castillo
<ibc@aliax.net>

[toc] | [prev] | [next] | [standalone]


#2613

FromRobert Klemme <shortcutter@googlemail.com>
Date2011-04-11 02:52 -0500
Message-ID<BANLkTin2tV=Hn+usVQqbAY9=sp2-kZGL5w@mail.gmail.com>
In reply to#2611
On Mon, Apr 11, 2011 at 9:30 AM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
> 2011/4/8 7stud -- <bbxx789_05ss@yahoo.com>:
>> 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
>
> Great solution :)

You could even use #equal? instead of #eql? just in case #eql? is overridden.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web