# Actors and Concurrency

**URL:** <https://rubytalk.org/t/actors-and-concurrency/44432>\
**Category:** ruby-talk\
**Created:** [11 February 2008 23:18 UTC](https://rubytalk.org/t/actors-and-concurrency/44432 "2008-02-11T23:18:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedzor](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@fedzor](https://rubytalk.org/u/fedzor)\
**Post date:** [11 February 2008 23:18 UTC](https://rubytalk.org/t/actors-and-concurrency/44432/1 "2008-02-11T23:18:03Z")

</div>

So I was messing with actors today, and I found that I couldn't get an actor to run concurrently with the main thread. Am I just wishing fibers are something they're not?

I had:

Actor.spawn  
&nbsp;&nbsp;&nbsp;sleep 1  
&nbsp;&nbsp;&nbsp;puts 5  
end

puts 6

and it went:  
5  
6

So what do I have to do to get Actors to go concurrent on my ass? I was hoping for:  
6  
5

Thanks,  
Ari  
--------------------------------------------|  
If you're not living on the edge,  
then you're just wasting space.

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [11 February 2008 23:59 UTC](https://rubytalk.org/t/actors-and-concurrency/44432/2 "2008-02-11T23:59:47Z")

</div>

To a point. However, note that if you want an actor to sleep for one second  
and permit other actors to proceed, you can just receive with a timeout of  
one second, rather than using sleep. Receive is how revactor actors transfer  
control to one another.

Of course, if the main thread exits (as it would in your code), then the  
spawned actor won't have a chance to finish.

-mental

> **···**
>
> On Tue, 12 Feb 2008 08:18:03 +0900, fedzor \<fedzor@gmail.com\> wrote:
> 
> > So I was messing with actors today, and I found that I couldn't get  
> > an actor to run concurrently with the main thread. Am I just wishing  
> > fibers are something they're not?

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [12 February 2008 00:00 UTC](https://rubytalk.org/t/actors-and-concurrency/44432/3 "2008-02-12T00:00:38Z")

</div>

(others can get more specific here, but here's the general idea)

First of all, Ruby does not have concurrency. Not even 1.9 allows  
concurrently running threads. The Ruby VM is a preemptive kernel.  
Eventually Ruby's threads will be system threads \*and\* fully  
concurrent, but that's a long time from now.

Second, Actors aren't really threads or Fibers. There's a single  
"dispatcher" thread/process/whatever that handles sending out events  
to the pool of actors. So while Actors may look like threads and  
concurrency, they aren't.

So put the two together, and you're going to get sequential code execution.

Jason

> **···**
>
> On Feb 11, 2008 6:18 PM, fedzor \<fedzor@gmail.com\> wrote:
> 
> > So I was messing with actors today, and I found that I couldn't get  
> > an actor to run concurrently with the main thread. Am I just wishing  
> > fibers are something they're not?
> > 
> > I had:
> > 
> > Actor.spawn  
> > &nbsp;&nbsp;&nbsp;sleep 1  
> > &nbsp;&nbsp;&nbsp;puts 5  
> > end
> > 
> > puts 6
> > 
> > and it went:  
> > 5  
> > 6
> > 
> > So what do I have to do to get Actors to go concurrent on my ass? I  
> > was hoping for:  
> > 6  
> > 5
> > 
> > Thanks,  
> > Ari  
> > --------------------------------------------|  
> > If you're not living on the edge,  
> > then you're just wasting space.

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [12 February 2008 00:09 UTC](https://rubytalk.org/t/actors-and-concurrency/44432/4 "2008-02-12T00:09:48Z")

</div>

Not necessarily true. Different actor implementations may have different  
scheduling behavior. For example, if you're using actors from the Omnibus  
library, you get a thread per actor, each of which handles its own event  
processing. On JRuby, that means you get a native thread per actor.

On the other hand, a native thread per actor isn't a good way to achieve  
performance.

-mental

> **···**
>
> On Tue, 12 Feb 2008 09:00:38 +0900, "Jason Roelofs" \<jameskilton@gmail.com\> wrote:
> 
> > Second, Actors aren't really threads or Fibers. There's a single  
> > "dispatcher" thread/process/whatever that handles sending out events  
> > to the pool of actors. So while Actors may look like threads and  
> > concurrency, they aren't.

---

<div class="post-metadata">

**Author:** ![fedzor](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@fedzor](https://rubytalk.org/u/fedzor)\
**Post date:** [12 February 2008 02:12 UTC](https://rubytalk.org/t/actors-and-concurrency/44432/5 "2008-02-12T02:12:49Z")

</div>

Can you give me some example code of working with receive? I kind of understand, but feel like some code would really nail it in.

Thanks,  
Ari  
-------------------------------------------|  
Nietzsche is my copilot

> **···**
>
> On Feb 11, 2008, at 6:59 PM, MenTaLguY wrote:
> 
> > On Tue, 12 Feb 2008 08:18:03 +0900, fedzor \<fedzor@gmail.com\> wrote:
> > 
> > > So I was messing with actors today, and I found that I couldn't get  
> > > an actor to run concurrently with the main thread. Am I just wishing  
> > > fibers are something they're not?
> > 
> > To a point. However, note that if you want an actor to sleep for one second  
> > and permit other actors to proceed, you can just receive with a timeout of  
> > one second, rather than using sleep. Receive is how revactor actors transfer  
> > control to one another.
> > 
> > Of course, if the main thread exits (as it would in your code), then the  
> > spawned actor won't have a chance to finish.

---

<div class="post-metadata">

**Author:** ![fedzor](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@fedzor](https://rubytalk.org/u/fedzor)\
**Post date:** [12 February 2008 02:14 UTC](https://rubytalk.org/t/actors-and-concurrency/44432/6 "2008-02-12T02:14:26Z")

</div>

I understand that there isn't really \*true\* concurrency, but is there any possible way to get, for example, my previous program to print "6\n5" instead of "5\n6"? I was hoping for some sort of Thread-like concurrency, but apparently am just wishing.

~ Ari  
English is like a pseudo-random number generator - there are a bajillion rules to it, but nobody cares.

> **···**
>
> On Feb 11, 2008, at 7:00 PM, Jason Roelofs wrote:
> 
> > (others can get more specific here, but here's the general idea)
> > 
> > First of all, Ruby does not have concurrency. Not even 1.9 allows  
> > concurrently running threads. The Ruby VM is a preemptive kernel.  
> > Eventually Ruby's threads will be system threads \*and\* fully  
> > concurrent, but that's a long time from now.
> > 
> > Second, Actors aren't really threads or Fibers. There's a single  
> > "dispatcher" thread/process/whatever that handles sending out events  
> > to the pool of actors. So while Actors may look like threads and  
> > concurrency, they aren't.
> > 
> > So put the two together, and you're going to get sequential code execution.

---

<div class="post-metadata">

**Author:** ![Leonard\_Cuff](https://avatars.discourse-cdn.com/v4/letter/l/a88e57/32.png) [@Leonard\_Cuff](https://rubytalk.org/u/Leonard_Cuff)\
**Post date:** [13 February 2008 19:28 UTC](https://rubytalk.org/t/actors-and-concurrency/44432/7 "2008-02-13T19:28:33Z")

</div>

I'm a newbie, so please set me straight. I've seen several people make the  
statement that 'Ruby does not have concurrency'. If this is so, what is the  
Threads class all about? And what is happening in this piece of code...Is  
this somehow the appearance of concurrency without being 'real' concurrency?  
What are the limitations here?

Thanks!

Leonard

#!/usr/bin/env ruby

&nbsp;&nbsp;&nbsp;&nbsp;# Fetch three urls simultaneously

&nbsp;&nbsp;&nbsp;&nbsp;require 'net/http'

&nbsp;&nbsp;&nbsp;&nbsp;pages = %w( [www.rubycentral.com](http://www.rubycentral.com)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;www.google.com  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;www.pragprog.com  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)

&nbsp;&nbsp;&nbsp;&nbsp;threads =

&nbsp;&nbsp;&nbsp;&nbsp;for page in pages  
&nbsp;&nbsp;&nbsp;&nbsp;threads \<\< Thread.new(page) { |myPage|

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h = Net::HTTP.new(myPage, 80)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Fetching: #{myPage}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resp, data = h.get('/', nil )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Got #{myPage}: #{resp.message}"  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;threads.each { |aThread| aThread.join }

Fetching: [www.rubycentral.com](http://www.rubycentral.com)  
Fetching: [www.google.com](http://www.google.com)  
Fetching: [www.pragprog.com](http://www.pragprog.com)  
Got [www.google.com](http://www.google.com): OK  
Got [www.pragprog.com](http://www.pragprog.com): OK  
Got [www.rubycentral.com](http://www.rubycentral.com): OK

> **···**
>
> On 2/11/08 4:00 PM, "Jason Roelofs" \<jameskilton@gmail.com\> wrote:
> 
> > (others can get more specific here, but here's the general idea)
> > 
> > First of all, Ruby does not have concurrency. Not even 1.9 allows  
> > concurrently running threads. The Ruby VM is a preemptive kernel.  
> > Eventually Ruby's threads will be system threads \*and\* fully  
> > concurrent, but that's a long time from now.
> > 
> > Second, Actors aren't really threads or Fibers. There's a single  
> > "dispatcher" thread/process/whatever that handles sending out events  
> > to the pool of actors. So while Actors may look like threads and  
> > concurrency, they aren't.
> > 
> > So put the two together, and you're going to get sequential code execution.
> > 
> > Jason
> > 
> > On Feb 11, 2008 6:18 PM, fedzor \<fedzor@gmail.com\> wrote:
> > 
> > > So I was messing with actors today, and I found that I couldn't get  
> > > an actor to run concurrently with the main thread. Am I just wishing  
> > > fibers are something they're not?
> > > 
> > > I had:
> > > 
> > > Actor.spawn  
> > > &nbsp;&nbsp;&nbsp;sleep 1  
> > > &nbsp;&nbsp;&nbsp;puts 5  
> > > end
> > > 
> > > puts 6
> > > 
> > > and it went:  
> > > 5  
> > > 6
> > > 
> > > So what do I have to do to get Actors to go concurrent on my ass? I  
> > > was hoping for:  
> > > 6  
> > > 5
> > > 
> > > Thanks,  
> > > Ari  
> > > --------------------------------------------|  
> > > If you're not living on the edge,  
> > > then you're just wasting space.

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [13 February 2008 19:40 UTC](https://rubytalk.org/t/actors-and-concurrency/44432/8 "2008-02-13T19:40:31Z")

</div>

Yeah, sorry, it can be confusing.

Ruby has threads, and they do run separate execution paths. However,  
these threads do not run \*at the same time\*. The Ruby 1.8 VM is a  
preemptive scheduler, in that it gives Thread 1 x milliseconds of  
execution time, then Thread 2, then Thread 3, looping back to Thread  
1. So in the end it looks as if they are running simultaneously, but  
they in fact are not.

This is actually the same in Ruby 1.9, except instead of Green threads  
(built, managed, and controlled exclusively by Ruby), it uses System  
threads (pthreads), but it still controls the thread execution to be  
the same as 1.8 Green threads.

I hope I explained that clearly enough. Please say if you're still confused.

Jason

> **···**
>
> On Feb 13, 2008 2:28 PM, Leonard Cuff \<lcuff@valueclick.com\> wrote:
> 
> > I'm a newbie, so please set me straight. I've seen several people make the  
> > statement that 'Ruby does not have concurrency'. If this is so, what is the  
> > Threads class all about? And what is happening in this piece of code...Is  
> > this somehow the appearance of concurrency without being 'real' concurrency?  
> > What are the limitations here?
> > 
> > Thanks!
> > 
> > Leonard
> > 
> > #!/usr/bin/env ruby
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;# Fetch three urls simultaneously
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;require 'net/http'
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;pages = %w( [www.rubycentral.com](http://www.rubycentral.com)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;www.google.com  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;www.pragprog.com  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;threads =
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;for page in pages  
> > &nbsp;&nbsp;&nbsp;&nbsp;threads \<\< Thread.new(page) { |myPage|
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h = Net::HTTP.new(myPage, 80)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Fetching: #{myPage}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resp, data = h.get('/', nil )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Got #{myPage}: #{resp.message}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;threads.each { |aThread| aThread.join }
> > 
> > Fetching: [www.rubycentral.com](http://www.rubycentral.com)  
> > Fetching: [www.google.com](http://www.google.com)  
> > Fetching: [www.pragprog.com](http://www.pragprog.com)  
> > Got [www.google.com](http://www.google.com): OK  
> > Got [www.pragprog.com](http://www.pragprog.com): OK  
> > Got [www.rubycentral.com](http://www.rubycentral.com): OK
> > 
> > On 2/11/08 4:00 PM, "Jason Roelofs" \<jameskilton@gmail.com\> wrote:
> > 
> > \> (others can get more specific here, but here's the general idea)  
> > \>  
> > \> First of all, Ruby does not have concurrency. Not even 1.9 allows  
> > \> concurrently running threads. The Ruby VM is a preemptive kernel.  
> > \> Eventually Ruby's threads will be system threads \*and\* fully  
> > \> concurrent, but that's a long time from now.  
> > \>  
> > \> Second, Actors aren't really threads or Fibers. There's a single  
> > \> "dispatcher" thread/process/whatever that handles sending out events  
> > \> to the pool of actors. So while Actors may look like threads and  
> > \> concurrency, they aren't.  
> > \>  
> > \> So put the two together, and you're going to get sequential code execution.  
> > \>  
> > \> Jason  
> > \>  
> > \> On Feb 11, 2008 6:18 PM, fedzor \<fedzor@gmail.com\> wrote:  
> > \>\> So I was messing with actors today, and I found that I couldn't get  
> > \>\> an actor to run concurrently with the main thread. Am I just wishing  
> > \>\> fibers are something they're not?  
> > \>\>  
> > \>\> I had:  
> > \>\>  
> > \>\> Actor.spawn  
> > \>\> sleep 1  
> > \>\> puts 5  
> > \>\> end  
> > \>\>  
> > \>\> puts 6  
> > \>\>  
> > \>\> and it went:  
> > \>\> 5  
> > \>\> 6  
> > \>\>  
> > \>\> So what do I have to do to get Actors to go concurrent on my ass? I  
> > \>\> was hoping for:  
> > \>\> 6  
> > \>\> 5  
> > \>\>  
> > \>\> Thanks,  
> > \>\> Ari  
> > \>\> --------------------------------------------|  
> > \>\> If you're not living on the edge,  
> > \>\> then you're just wasting space.  
> > \>\>  
> > \>\>  
> > \>\>  
> > \>\>  
> > \>

---

<div class="post-metadata">

**Author:** ![MenTaLguY1](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MenTaLguY1](https://rubytalk.org/u/MenTaLguY1)\
**Post date:** [13 February 2008 19:42 UTC](https://rubytalk.org/t/actors-and-concurrency/44432/9 "2008-02-13T19:42:24Z")

</div>

Unfortunately, people are confusing concurrency with simultaneity. Most  
Ruby implementations (JRuby is an exception) do not permit Ruby threads  
to run truly simultaneously (e.g. on multiple processors); instead,  
their execution is simply interleaved in a non-deterministic fashion.

However, this is still concurrency in the technical sense of the term.

-mental

> **···**
>
> On Thu, 14 Feb 2008 04:28:33 +0900, Leonard Cuff \<lcuff@valueclick.com\> wrote:
> 
> > I'm a newbie, so please set me straight. I've seen several people make the  
> > statement that 'Ruby does not have concurrency'. If this is so, what is  
> > the Threads class all about? And what is happening in this piece of code...Is  
> > this somehow the appearance of concurrency without being 'real'  
> > concurrency?
