Posty

Wyświetlanie postów z luty, 2012

[PL] Nothing about programming

Obraz

Everything You Always Wanted to Know About Fibers * But Were Afraid to Ask

I recently found out that most Ruby developers that are not familiar with Event Machine usually knows very little about fibers. So I decided to share some very trivial examples that will help you understand them quickly. Each example should push your knowledge and imagination about possible usages a little further. Tell me in comments if that worked for you.

Example 1:


What happened here, you may ask. We created new fiber. At the begining it was stopped. So we resumed it. It executed and ended. Trying to resume it again did not work. Actually that was very similar to Proc except that we can always call Proc multiple times.

Example 2:


While being inside fiber we can yield. That means that fiber stops and control goes back to the line that resumed fiber. We resumed the fiber second time starting from where we escaped (yielded) previously.

Example 3:


As you see, you can return value from fiber when yielding out. That value is accessible to code that called fiber (resumed it). How is it acce…