0

I wrote this code about ruby thread to open 50 threads and every thread wait for 2s.

#!/home/sun/.rvm/rubies/ruby-1.9.3-p448/bin/ruby
ts = []
50.times do |p|
    ts << Thread.new do
        sum = 0
        5.times do |i|
            sleep(2)
        end
        puts "turn "+p.to_s+" finished"
    end
end

ts.each {|x| x.join}

and to compare with ruby eventmachine, i can't use sleep in EM.do , because it will block the reactor of eventmachine.

so I tried to write code below:

#!/home/sun/.rvm/rubies/ruby-1.9.3-p448/bin/ruby
require 'eventmachine'
ts = []
EM.run do
    q = 0
    def dfs(tm)
        return 0 if tm == 0
        EM.add_timer(2) do
            dfs(tm-1)
        end
    end
    50.times do |p|
        op = proc do
            dfs(5)
        end
        callback = proc do 
            puts "turn "+p.to_s+" finished"
            q += 1
            if q == 50 
                EM.stop
            end
        end
        EM.defer(op,callback)
    end
end

But it runs over in just 1s. I don't know how to code to let it wait some seconds in every eventmachine loop.

Can anybody give some help? Thanks!

wonderflow
  • 665
  • 2
  • 7
  • 18
  • What are you trying to determine? EventMachine, by default, only has 20 threads in it's thread pool. Why do you want to know how EM's threads compare to Ruby threads? (Internally, EM's thread pool is just a bunch of Thread.new's) – dj2 Oct 04 '13 at 03:06
  • I find a lot of blogs saying eventmachine is more effecient than ruby thread. But I find ruby thread works well. so I hope to test them. – wonderflow Oct 11 '13 at 08:56

2 Answers2

0

Eventmachine is single-threaded. You can't compare them. If you want to test real situation then you can try DB 'sleep'. e.g. mongo http://docs.mongodb.org/manual/reference/command/sleep/ . But use EM library for request. e.g. https://github.com/bcg/em-mongo

Sergii Mostovyi
  • 1,361
  • 1
  • 15
  • 19
  • Thank you. And do you know an easy way to compare the efficiency between eventmachine and thread of ruby ? – wonderflow Sep 30 '13 at 05:49
  • How is Eventmachine single threaded while utilising a threadpool? – bernstein7 Aug 30 '22 at 11:12
  • @bernstein7 it doesn't utilize ruby-level threads. It uses system-based operations to get back to a network file descriptor when it's ready. It uses epoll https://man7.org/linux/man-pages/man7/epoll.7.html – Sergii Mostovyi Sep 27 '22 at 12:09
0

The efficiency will be dependant on your workload. If you're doing a lot of IO work then EM has a good chance of winning as you won't incur the cost of context switches. There is a lot of time spent idle when doing IO.

On the other hand, if you're doing compute intensive work, then it, again, depends. EM has a thread pool with 20 threads, if you have more then 20 parallel computations then spawning the right number of Ruby threads will be better.

EM threads are just Ruby threads. They're kept in a thread pool so you save a bit of overhead starting/stopping the thread as it executes. EM also handles passing work out to the threads when you've asked for more then 20 items of work.

If you want to compare them, just create an EM program and a threaded program that are the same that model your workload. Then time how long they take to finish.

dj2
  • 9,534
  • 4
  • 29
  • 52
  • Thank you. But can you show me some evidences about "EM threads are just Ruby threads. " Someone told me EM threads use a different way associated with linux kernal! – wonderflow Oct 12 '13 at 01:54
  • https://github.com/eventmachine/eventmachine/blob/master/lib/eventmachine.rb#L1040 – dj2 Oct 12 '13 at 12:57