From: rubybugs@... Date: 2016-11-23T19:16:18+00:00 Subject: [ruby-core:78280] [Ruby trunk Feature#3944] Add Fiber#root? method Issue #3944 has been updated by Christopher Aue. I also need Fiber#root?. My Scenario looks similar to the ones above, but I'm not using EM: I have a loop watching sockets. This loop runs in a fiber. Each time a socket is readable another fiber is created to handle the request. So, if I have a server only listening for requests: 1) I resume (i.e. start) the loop fiber, 2) resume (i.e. start) a dedicated fiber for each request, 3) yield to the loop fiber when the request itself needs to send a request to another server and must wait for the response, 4) resume the request fiber again once the response is there, 5) go to step 3) until the request is fully handled 6) yield to the main loop and go to step 1) The loops starts at the beginning and runs forever. But that is what I expect a server to do. No problem. But there is a second scenario, when I have a linear script with start and finish. This script uses the same code as the server above and during its course calls the same methods the server uses to handle its requests. Calling these methods sends the same requests to another server for which responses the script needs to wait. After the request is fully processed the script continues. Then the flow looks like this: 1) I'm processing the script on the root fiber. 2) When a request in need of a response from a server is encountered, I'm actually at a point corresponding to step 3) from above. But, I cannot yield back to the loop since it has not been started yet. I have to resume it. 3) From here, I'm at step 1) from above and can proceed accordingly. 4) Once my request is fully handled I need to yield from the loop to the root fiber to continue the script. To decide if I need to resume the loop or yield to it, I need to know if the request originated from the root fiber or from one of the request fibers of the loop. For a solution now, I have created a small gem monkey patching Thread and Fiber to get the root fiber: https://github.com/christopheraue/ruby-thread_root_fiber ---------------------------------------- Feature #3944: Add Fiber#root? method https://bugs.ruby-lang.org/issues/3944#change-61647 * Author: Mike Perham * Status: Feedback * Priority: Normal * Assignee: Koichi Sasada ---------------------------------------- Since the root fiber is treated differently than other Fibers (e.g. you can't yield from the root), code which can optionally use fibers for high-performance IO (in my case, the Mysql2 driver) needs `Fiber#root?` to know when it can use Fibers to execute a query versus using a standard blocking call. -- https://bugs.ruby-lang.org/ Unsubscribe: