使用Soap4r在ruby中锁定线程 [英] Thread lockup in ruby with Soap4r
问题描述
这与我在这里提出的问题有关: Ruby中的线程锁定(使用soap4r和QT)
This is related to a question I asked here: Thread Locking in Ruby (use of soap4r and QT)
但是,它对于该问题的一部分来说是特定的,并且得到了一个更简单的示例的支持.测试代码为:
However it is particular to one part of that question and is supported by a simpler example. The test code is:
require 'rubygems'
require 'thread'
require 'soap/rpc/standaloneserver'
class SOAPServer < SOAP::RPC::StandaloneServer
def initialize(* args)
super
# Exposed methods
add_method(self, 'test', 'x', 'y')
end
def test(x, y)
return x + y
end
end
myServer = SOAPServer.new('monitorservice', 'urn:ruby:MonitorService', 'localhost', 4004)
Thread.new do
puts 'Starting web services'
myServer.start
puts 'Ending web services'
end
sleep(4)
#Thread.new do
testnum = 0
while testnum < 4000 do
testnum += 1
puts myServer.test(0,testnum)
sleep(2)
end
#end
puts myServer.test(0,4001)
puts myServer.test(0,4002)
puts myServer.test(0,4003)
puts myServer.test(0,4004)
gets
当我在线程注释掉的情况下运行此命令时,一切运行正常.但是,一旦将线程放入进程中就会挂起.我戳入Webrick,发现此处出现止损(推杆当然是我的):
When I run this with the thread commented out everything runs along fine. However, once the thread is put in the process hangs. I poked into Webrick and found that the stop occurs here (the puts are, of course, mine):
while @status == :Running
begin
puts "1.1"
if svrs = IO.select(@listeners, nil, nil, 2.0)
svrs[0].each{|svr|
puts "-+-"
@tokens.pop # blocks while no token is there.
if sock = accept_client(svr)
th = start_thread(sock, &block)
th[:WEBrickThread] = true
thgroup.add(th)
else
@tokens.push(nil)
end
}
end
puts ".+."
在未注释掉线程的情况下运行时,我得到如下信息: 启动Web服务
When run with the thread NOT commented out I get something like this: Starting web services
1.1
.+.
1.1
4001
4002
4003
4004
1
.+.
1.1
推荐答案
结尾处会阻塞Ruby的IO.我不知道为什么.如果将其替换为几乎所有内容,则该程序均可运行.我使用了一个睡眠循环:
The trailing gets blocks Ruby's IO. I'm not sure why. If it is replaced with pretty much anything the program works. I used a sleeping loop:
loop do
sleep 1
end
已添加: 我应该注意,基于睡眠增量,我也会在睡眠中出现奇怪的行为.最后,我放弃了Ruby,因为线程行为太不可思议了.
ADDED: I should note that I also get strange behavior with sleep based on the sleep increment. In the end I abandoned Ruby since the threading behavior was too wonky.
这篇关于使用Soap4r在ruby中锁定线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!