开发者

Ruby Shoes and MySQL: GUI freezes, should I use threads?

开发者 https://www.devze.com 2023-03-13 04:54 出处:网络
I\'m trying to learn Shoes and decided to make a simple GUI to run a SQL-script line-by-line. M开发者_运维技巧y problem is that in GUI pressing the button, which executes my function, freezes the GUI

I'm trying to learn Shoes and decided to make a simple GUI to run a SQL-script line-by-line.

M开发者_运维技巧y problem is that in GUI pressing the button, which executes my function, freezes the GUI for the time it takes the function to execute the script.

With long scripts this might take several minutes.

Someone had a similar problem (http://www.stackoverflow.com/questions/958662/shoes-and-heavy-operation-in-separate-thread) and the suggestion was just to put intensive stuff under a Thread: if I copy the math-code from previously mentioned thread and replace my mysql-code the GUI works without freezing, so that probably hints to a problem with how I'm using the mysql-adapter?

Below is a simplified version of the code:

problem.rb:


# copy the mysql-gem if not in gem-folder already
Shoes.setup do
  gem 'mysql'
end

require "mysql"

# the function that does the mysql stuff
def someFunction

  con = Mysql::real_connect("myserver", "user", "pass", "db")

  scriptFile = File.open("myfile.sql", "r")
  script = scriptFile.read
  scriptFile.close
  result = []

  script.each_line do |line| 
    result << con.query(line)
  end

  return result
end

# the Shoes app with the Thread
Shoes.app do
  stack do
    button "Execute someFunction" do
      Thread.start do
        result = someFunction
        para "Done!"
      end
    end
  end  
  stack do
    button "Can't click me when GUI is frozen" do
      alert "GUI wasn't frozen?"
    end
  end
end


I think the problem arises from scheduling which is done by ruby, not by the operating system. Probably just a special case with shoes + mysql.

As a workaround i'd suggest you spawn a separate process for the script and use socket or file based communication between the processes.

0

精彩评论

暂无评论...
验证码 换一张
取 消