Ruby Forum wxRuby > Treads slowing down the process

Posted by Joseph Edelstein (jedelstein)
on 01.07.2008 21:04
Attachment: bogus_conts.rb (41,9 KB)
Hello,

I was having problems with my GUI freezing if you clicked off of it when
it was running or if it was processing a large file.  After researching
the forum, I found a thread that dealt with the problem by adding a
timer and Thread.pass to the App.  This solved the problem of the GUI
freezing or hanging.

However, it has caused the program to run very slowly.  What use to take
only a few seconds now takes a few minutes.  People will be processing
large text files with this program so speed is important.

Thanks in advance for any help.

Joe

Ruby code:

begin
  require 'rubygems'
rescue LoadError
end

require 'rubyscript2exe'
require 'wx'
include Wx
require 'Bogus_Frame.rb'
require 'bogus_conts.rb'
include Process

RUBYSCRIPT2EXE.lib = ["Bogus_GUI.xrc"]
RUBYSCRIPT2EXE.lib = ['Bogus_Frame.rb']
RUBYSCRIPT2EXE.lib = ['bogus_conts.rb']

class BogusFrame < BogusBase
  include Bogus_code
  def initialize
    @ext_def_source = ".txt"
    @ext_def_result = ".txt"
    super
    evt_button(source_button) {|event| source(event)}
    evt_button(result_button) {|event| result(event)}
    evt_button(start_button) {|event| main}
  end#def initialize

  def source(event)
    source_path = file_selector(
                          "Select the file to load",
                          "", "",
                          @ext_def_source,
                          "CSV (*.csv)|*.csv|Plain text
(*.txt)|*.txt|All files (*.*)|*.*",
                          CHANGE_DIR,
                          self
                        )
    if source_path == nil
      return nil
    else
    @source_textbox.clear
    @source_textbox.append_text(source_path)
    end
  @ext_def_source = source_path[/[^\.]*$/]
  end#end def source

  def result(event)
    result_path = file_selector(
                          "Select the file to load",
                          "", "",
                          @ext_def_result,
                          "CSV (*.csv)|*.csv|Plain text
(*.txt)|*.txt|All files (*.*)|*.*",
                          CHANGE_DIR,
                          self
                        )
    if result_path == nil
      return nil
    end
    # it is just a sample, would use SplitPath in real program
    @ext_def_result = result_path[/[^\.]*$/]
    @result_textbox.clear
    @result_textbox.append_text(result_path)

  end#end def source


end#end Class

class BogusApp < App
  def on_init()
    t = Wx::Timer.new(self, 55)
     evt_timer(55) { Thread.pass }
     t.start(1)
    BogusFrame.new.show

  end
end

BogusApp.new.main_loop()
Posted by Mario Steele (Guest)
on 02.07.2008 08:12
(Received via mailing list)
Hello Joseph,

There is a certain problem with this.  Threads tend to make things a bit
slower with processing, due to the very nature of Threads.  There is
literally a pause current process, switch to next to give shared time,
before switching back.  So what may only take a few seconds, can 
increase
when you involve Threads.  The thing you have to remember, is that 
Threads
are green in Ruby.  Meaning they are software controlled, not Hardware
controlled.  As it is, what you are currently doing, to execute the 
Timer
every millisecond, is about the best you can do, to increase the speed. 
So
unfortunately, there's not much else here that can be solved.  Unless 
you
wish to get into separating the code that processes the Text file, into 
a
separate file, and using something like Open3 to call the second bit of
code, and monitor the output from the sub-process.

That will be the only way you could truly get close-as-possible 
threading in
Ruby.

On Tue, Jul 1, 2008 at 2:04 PM, Joseph Edelstein <lists@ruby-forum.com>