Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:02:17+00:00 2026-06-12T08:02:17+00:00

I’m running this code snippet under Ruby 1.9.2: require eventmachine require fiber EM.run do

  • 0

I’m running this code snippet under Ruby 1.9.2:

require "eventmachine"
require "fiber"

EM.run do
  fiber = Fiber.new do
    current_fiber = Fiber.current
    EM.add_timer(2) do
      print "B"
      current_fiber.resume("D")
    end
    Fiber.yield
  end
  print "A"
  val = fiber.resume
  print "C"
  print val
  EM.stop
end

I’m expecting the output to be “ABCD”, with the program pausing for two seconds after the “A”. However, instead it just prints out “AC” right away, then waits around for two seconds before exiting. What am I doing wrong?

(For reference, I’m trying to reproduce the em-synchrony-style behaviour described in this article without using em-synchrony.)

Edit: Here are some more details about what I’m ultimately trying to accomplish. I’m developing a Grape API running on Thin, and each route handler has to make various calls in series to datastores, ZooKeeper, other HTTP services, etc. before returning a response.

em-synchrony is really cool, but I keep running into issues with yielding from the root fiber or with results showing the non-synchronous symptoms of the case above. rack-fiber_pool also seems potentially useful, but I’m reluctant to commit to using it because, out of the box, it breaks all my Rack::Test unit tests.

I reduced my problems into the simple example above because I seem to have a fundamental misunderstanding about how fibers and EventMachine should be used together that is preventing me from using the more complex frameworks effectively.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:02:18+00:00Added an answer on June 12, 2026 at 8:02 am

    You probably wanted something like this:

    require "eventmachine"
    require "fiber"
    
    def value
      current_fiber = Fiber.current
    
      EM.add_timer(2) do
        puts "B"
        current_fiber.resume("D") # Wakes the fiber
      end
    
      Fiber.yield # Suspends the Fiber, and returns "D" after #resume is called
    end
    
    EM.run do
      Fiber.new {
        puts "A"
        val = value
        puts "C"
        puts val
    
        EM.stop
      }.resume
    
      puts "(Async stuff happening)"
    end
    

    This should yield the following result:

    A
    (Async stuff happening)
    B
    C
    D
    

    A more conceptual explanation:

    Fibers help untangle asynchronous code because they chunks of code to be suspended and reanimated, much like manual threads. This allows for clever tricks regarding the order on which things happen. A small example:

    fiberA = Fiber.new {
      puts "A"
      Fiber.yield
      puts "C"
    }
    
    fiberB = Fiber.new {
      puts "B"
      Fiber.yield
      puts "D"
    }
    
    fiberA.resume # prints "A"
    fiberB.resume # prints "B"
    fiberA.resume # prints "C"
    fiberB.resume # prints "D"
    

    So, when #resume is called on a fiber, it resumes its execution, be it from the start of the block (for new fibers), or from a previous Fiber.yield call, and then it executes until another Fiber.yield is found or the block ends.

    It is important to note that placing a sequence of actions inside a fiber is a way to state a temporal dependency between them (puts "C" can’t run before puts "A"), while actions on “parallel” fibers can’t count on (and shouldn’t care about) whether or not the actions on the other fibers have executed: We would print “BACD” only by swapping the first two resume calls.

    So, here’s how rack-fiber_pool does its magic: It places every request your application receives inside a fiber (which implies order-independence), and then expects you to Fiber.yield on IO actions, so that the server can accept other requests. Then, inside the EventMachine callbacks, you pass in a block that contains a current_fiber.resume, so that your fiber is reanimated when the answer to the query/request/whatever is ready.

    This is already a lengthy answer, but I can provide an EventMachine example if it’s still not clear (I get this is a hairy concept to grok, I struggled a lot).


    Update: I’ve created an example that might help anyone that is still struggling with the concepts: https://gist.github.com/renato-zannon/4698724. I recommend to run and play with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.