# Asynchronicity

**URL:** https://rubytalk.org/t/asynchronicity/62888
**Category:** ruby-talk
**Created:** [8 June 2011 17:52 UTC](https://rubytalk.org/t/asynchronicity/62888 "2011-06-08T17:52:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Iain\_Barnett](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/iain_barnett/32/2042_2.png) [@Iain\_Barnett](https://rubytalk.org/u/Iain_Barnett)
#### Post date: [8 June 2011 17:52 UTC](https://rubytalk.org/t/asynchronicity/62888/1 "2011-06-08T17:52:57Z")

</div>

Hi all,

Just wondering if anyone can recommend some articles/tutorials on fibers and async stuff? I've had a look around of course, but it always seems to be the same example of an HTTP server, or some explanation of theory. Call me an ignoramus (go on, it's relaxing) but I don't really care about the theory, some good examples moving from the simple to the not so simple in nice gradations would be muy bueno!

If it links in nicely with Eventmachine and/or Thin, all the better.

Regards,  
Iain

---

<div class="post-metadata">

### Author: ![John\_W\_Higgins1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/john_w_higgins1/32/1922_2.png) [@John\_W\_Higgins1](https://rubytalk.org/u/John_W_Higgins1)
#### Post date: [8 June 2011 18:02 UTC](https://rubytalk.org/t/asynchronicity/62888/2 "2011-06-08T18:02:24Z")

</div>

Good Morning,

> **···**
>
> On Wed, Jun 8, 2011 at 10:52 AM, Iain Barnett \<iainspeed@gmail.com\> wrote:
> 
> > Hi all,
> > 
> > Just wondering if anyone can recommend some articles/tutorials on fibers  
> > and async stuff? I've had a look around of course, but it always seems to be  
> > the same example of an HTTP server, or some explanation of theory. Call me  
> > an ignoramus (go on, it's relaxing) but I don't really care about the  
> > theory, some good examples moving from the simple to the not so simple in  
> > nice gradations would be muy bueno!
> > 
> > If it links in nicely with Eventmachine and/or Thin, all the better.
> 
> While not a tutorial - EM-Synchrony is an EventMachine/fiber based set of  
> classes that are a great example of putting fibers to use. Everything from  
> simple sleep and timers using EM/fibers to full blown http clients and tools  
> to wrap regular code to run async.
> 
> > **[GitHub - igrigorik/em-synchrony: Fiber aware EventMachine clients and...](https://github.com/igrigorik/em-synchrony)**
> >
> > Fiber aware EventMachine clients and convenience classes - GitHub - igrigorik/em-synchrony: Fiber aware EventMachine clients and convenience classes
> 
> John

---

<div class="post-metadata">

### Author: ![Kirk\_Haines2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/kirk_haines2/32/1992_2.png) [@Kirk\_Haines2](https://rubytalk.org/u/Kirk_Haines2)
#### Post date: [8 June 2011 19:51 UTC](https://rubytalk.org/t/asynchronicity/62888/3 "2011-06-08T19:51:58Z")

</div>

There isn't really a lot involved with fibers. Replace that HTTP  
request with anything else, and you get the same basic example.

There are some nuances between the use of #transfer and #resume, but  
the method count on Fiber is very small, so there isn't a lot to  
learn. Just take one of the examples from one of the existing articles  
and experiment a little bit, and you will have the topic well  
explored.

There's nothing extra that needs to happen to link it with  
EventMachine, either, since the Fiber API is already so shallow.

# 1) Create Fiber.  
Fiber.new do

&nbsp;&nbsp;&nbsp;&nbsp;# 2) Blah blah blah code that does whatever your code does.

&nbsp;&nbsp;&nbsp;&nbsp;# 3) Grab the current fiber.  
&nbsp;&nbsp;&nbsp;&nbsp;f = Fiber.current

&nbsp;&nbsp;&nbsp;&nbsp;# 4) Create some EventMachine protocol object that will do  
something for you.  
&nbsp;&nbsp;&nbsp;&nbsp;something\_doer = EventMachine::Doer.new(stuff).doit

&nbsp;&nbsp;&nbsp;&nbsp;# 5) Setup the callback to wake the fiber up when it's done.  
&nbsp;&nbsp;&nbsp;&nbsp;something\_doer.callback { f.resume (something\_doer) }

&nbsp;&nbsp;&nbsp;&nbsp;# 6) Suspend this fiber so that something else can run.  
&nbsp;&nbsp;&nbsp;&nbsp;Fiber.yield

&nbsp;&nbsp;&nbsp;&nbsp;# 7) More blah blah blah that you want to do after your  
EventMachine::Doer finishes it's stuff.

end

If you wrap #3-#6 in a method, then you can call into it via a nice  
clean API, treating it just like it were a simple blocking call, and  
whatever is doing it need not even know that it is an asynchronous,  
nonblocking call. As far as your code, and your thought process while  
writing the code is concerned, it will call into the method, and get a  
return value from the method. This is the strength of fibers when used  
with something like EventMachine, in my opinion. You can make your API  
simpler to understand by using fibers to hide the asynchronous nature  
of what is happening behind it.

Just work with that basic template, which is what you will find in  
most any example, and you should have it figured out pretty quickly.

I have a very simple presentation on this that I did for the  
EMRubyConf at RailsConf. I should find out if there is an official  
place for these things, but if you want to see it, you can get it  
here: [swiftcore.org&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;swiftcore Resources and Information.](http://kirk.swiftcore.org/emrubyconf_fibers_presentation.pdf)

Kirk Haines  
Developer  
Engine Yard

> **···**
>
> On Wed, Jun 8, 2011 at 11:52 AM, Iain Barnett \<iainspeed@gmail.com\> wrote:
> 
> > Hi all,
> > 
> > Just wondering if anyone can recommend some articles/tutorials on fibers and async stuff? I've had a look around of course, but it always  
> > seems to be the same example of an HTTP server, or some explanation of theory. Call me an ignoramus (go on, it's relaxing) but I don't  
> > really care about the theory, some good examples moving from the simple to the not so simple in nice gradations would be muy bueno!

---

<div class="post-metadata">

### Author: ![Iain\_Barnett](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/iain_barnett/32/2042_2.png) [@Iain\_Barnett](https://rubytalk.org/u/Iain_Barnett)
#### Post date: [8 June 2011 22:58 UTC](https://rubytalk.org/t/asynchronicity/62888/4 "2011-06-08T22:58:57Z")

</div>

> Good Morning,

I read that and got one of those "I'm speaking to someone on the other side of the planet, isn't the internet amazing!" moments 🙂

> **···**
>
> On 8 Jun 2011, at 19:02, John W Higgins wrote:
> 
> On 8 Jun 2011, at 20:51, Kirk Haines wrote:
> 
> > There isn't really a lot involved with fibers.
> 
> I hope so! 🙂
> 
> I've downloaded the pdf, and loaded up the Github, so I'll take a look and digest it all over the next couple of days and let you know how I get on. The first thing I want to use it for is to encrypt photos uploaded to a server without tying up every instance running.
> 
> I really appreciate you both taking the time to answer, thankyou.
> 
> Regards,  
> Iain
