gpt4 book ai didi

mysql - Ruby Shoes 和 MySQL : GUI freezes, 我应该使用线程吗?

转载 作者:行者123 更新时间:2023-11-29 00:55:20 24 4
gpt4 key购买 nike

我正在尝试学习 Shoes,并决定制作一个简单的 GUI 来逐行运行 SQL 脚本。

我的问题是,在 GUI 中按下执行我的功能的按钮时,GUI 卡住了函数执行脚本所需的时间。

对于长脚本,这可能需要几分钟时间。

有人有类似的问题 ( http://www.stackoverflow.com/questions/958662/shoes-and-heavy-operation-in-separate-thread ),建议只是将密集的东西放在一个线程下:如果我从前面提到的线程复制数学代码并替换我的 mysql 代码,GUI 可以工作而不会卡住,所以这可能暗示我使用 mysql 适配器的方式有问题?

下面是代码的简化版本:

问题.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

最佳答案

我认为问题出在由 ruby​​ 而不是操作系统完成的调度。可能只是鞋子+mysql的特例。

作为解决方法,我建议您为脚本生成一个单独的进程,并在进程之间使用基于套接字或文件的通信。

关于mysql - Ruby Shoes 和 MySQL : GUI freezes, 我应该使用线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6370291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com