[编辑注意:注意到我已将互斥锁创建放在构造函数中。移动它,并没有发现任何变化。]
[编辑说明 2:我在试运行中将对 app.exec 的调用更改为
while TRUE do
app.processEvents()
puts '."
end
我注意到一旦 Soap4r 服务开始运行,就不会再调用任何进程事件]
[编辑说明 3:在此处创建了一个相关问题:Thread lockup in ruby with Soap4r
我正在尝试编写一个 ruby 程序,它接收 SOAP 命令以在监视器上绘图(从而允许远程监视器访问)。我已经组装了一个简单的测试应用程序来原型化这个想法。图形工具包是 QT。我有我认为是锁定问题。我在所示代码中添加了测试服务器中方法的调用。我现在正在测试的服务器端是:
require 'rubygems'
require 'Qt4'
require 'thread'
require 'soap/rpc/standaloneserver'
class Box < Qt::Widget
def initialize(parent = nil)
super
setPalette(Qt::Palette.new(Qt::Color.new(250,0,0)))
setAutoFillBackground(true)
show
end
end
class SOAPServer < SOAP::RPC::StandaloneServer
@@mutex = Mutex.new
def initialize(* args)
super
# Exposed methods
add_method(self, 'createWindow', 'x', 'y', 'width', 'length')
end
def createWindow(x, y, width, length)
puts 'received call'
windowID = 0
puts @boxList.length
puts @parent
@@mutex.synchronize do
puts 'in lock'
box = Box.new(@parent)
box.setGeometry(x, y, width, length)
windowID = @boxList.push(box).length
print "This:", windowID, "--\n"
end
puts 'out lock'
return windowID
end
def postInitialize (parent)
@parent = parent
@boxList = Array.new
end
end
windowSizeX = 400
windowSizeY = 300
app = Qt::Application.new(ARGV)
mainwindow = Qt::MainWindow.new
mainwindow.resize(windowSizeX, windowSizeY)
mainwindow.show
puts 'Attempting server start'
myServer = SOAPServer.new('monitorservice', 'urn:ruby:MonitorService', 'localhost', 4004)
myServer.postInitialize(mainwindow)
Thread.new do
puts 'Starting?'
myServer.start
puts 'Started?'
end
Thread.new do
myServer.createWindow(10,0,10,10)
myServer.createWindow(10,30,10,10)
myServer.createWindow(10,60,10,10)
myServer.createWindow(10,90,10,10)
end
myServer.createWindow(10,10,10,10)
Thread.new do
app.exec
end
gets
现在,当我运行它时,我得到以下输出:
正在尝试启动服务器 正在
启动?
接收呼叫
0
#<Qt::MainWindow:0x60fea28>
锁定
接收呼叫
0
#<Qt::MainWindow:0x60fea28>
This:1--
锁定
This :2--
锁定
在那一点上,我挂起而不是收到我期望的总共五个添加。Qt 会显示由“createWindow(10,0,10,10)”和“createWindow(10,10,10,10)”定义的方块。鉴于 "This:1--" 和 "This:2--" 显示在下一个输入/输出锁对中,我假设我使用互斥锁非常错误。这是我第一次在 Ruby 中使用线程。