Actors and Concurrency

F

fedzor

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.
 
M

MenTaLguY

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.

-mental
 
J

Jason Roelofs

(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
 
M

MenTaLguY

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.

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
 
F

fedzor

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.

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
 
F

fedzor

(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.

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.
 
J

Jason Roelofs

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

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

# Fetch three urls simultaneously

require 'net/http'

pages = %w( www.rubycentral.com
www.google.com
www.pragprog.com
)

threads = []

for page in pages
threads << Thread.new(page) { |myPage|


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


threads.each { |aThread| aThread.join }


Fetching: www.rubycentral.com
Fetching: www.google.com
Fetching: www.pragprog.com
Got www.google.com: OK
Got www.pragprog.com: OK
Got www.rubycentral.com: OK




(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top