arirusso / alsa-rawmidi

Realtime MIDI IO with Ruby for Linux
Other
12 stars 8 forks source link

CPU hogging when waiting for MIDI input #2

Closed zarate closed 10 years ago

zarate commented 11 years ago

Hi there,

First of all, I'm not sure this is a bug or simply if it works as designed. Thought of raising it just in case. My configuration is MPB, Ubuntu 12.04, Ruby 1.9.3p327.

Consider the code below:

require 'unimidi'
input = UniMIDI::Input.first.open

Thread.new(){
    loop do
        puts "hello"
    end
}

loop do
    puts input.gets
end

This gets at least one of the cores consistently at 100% usage. I would have thought this might not be a big deal, but it really drags the second thread. I've tried setting a higher thread priority to no avail.

You can "workaround" by simply adding a sleep call to input.gets:

sleep 0.01

Now, I don't know much about handling MIDI, but looks like you would be losing data. Is this acceptable in applications where speed and precision is a must?

Cheers!

Juan

PS: it seems that this does not happen on Ruby 1.8.7, could it be a Ruby regression?

arirusso commented 11 years ago

@zarate Sorry for the slow reply, I didn't get an alert on this. Putting a sleep call there makes some sense-- that's how I handle it in the OSX version. Will try it out in the next few days...

zarate commented 11 years ago

@arirusso haha, ok! let me know how it goes.