R
Rickard Sjostrom
Hi!
I have a problem with hanging XMLRPC calls. It appears when I call
XMLRPC::Client.new too freqently. For my own sake, I have a work around
for this (I save the server reference in an instance variable, for
example).
But this might be of interest for other people. And my idea was to show
this for you Ruby people with more insight into this area.
As you can see below sometimes the call hangs for 30 seconds. Note that
it appears even when sleeping 5 seconds between the instantiations.
This is a really simple piece of code!
Best Regards
///Rickard
Here is the code:
================================
#!/app/ruby/1.8.x/bin/ruby
require "xmlrpc/client"
# Server code (from
http://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-ruby.html):
##!/app/ruby/1.8.x/bin/ruby
#require "xmlrpc/server"
#s = XMLRPC::Server.new(4001)
#
#class MyHandler
# def sumAndDifference(a, b)
# { "sum" => a + b, "difference" => a - b }
# end
#end
#
#s.add_handler("sample", MyHandler.new)
#s.serve
##end of server code
def call_server(sleeptime)
puts "**** Trying with sleeptime #{sleeptime} sec ****"
# Create server reference outside of loop (this works fine)
#server = XMLRPC::Client.new("127.0.0.1", "/", 4001)
for i in 1..7
# Create server reference inside of loop (this creates the problem,
when called frequently)
server = XMLRPC::Client.new("127.0.0.1", "/", 4001)
start = Time.now.tv_sec
result = server.call("sample.sumAndDifference", 5, 3)
total = Time.now.tv_sec - start
# print result and number of seconds spent during the remote call
puts "Result: #{result.inspect}. Time: #{total} sec"
# just to show we dont need to stress "that much"
# it seems that it doensnt hang when sleep >= 10 sec (on my linux
machine)
Kernel.sleep(sleeptime)
end
end
[0,5,7,10].each do |sleeptime|
call_server(sleeptime)
end
Here is the output:
================================
; ./client.rb
**** Trying with sleeptime 0 sec ****
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 30 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
**** Trying with sleeptime 5 sec ****
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 20 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 10 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
**** Trying with sleeptime 7 sec ****
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 4 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 2 sec
**** Trying with sleeptime 10 sec ****
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
I have a problem with hanging XMLRPC calls. It appears when I call
XMLRPC::Client.new too freqently. For my own sake, I have a work around
for this (I save the server reference in an instance variable, for
example).
But this might be of interest for other people. And my idea was to show
this for you Ruby people with more insight into this area.
As you can see below sometimes the call hangs for 30 seconds. Note that
it appears even when sleeping 5 seconds between the instantiations.
This is a really simple piece of code!
Best Regards
///Rickard
Here is the code:
================================
#!/app/ruby/1.8.x/bin/ruby
require "xmlrpc/client"
# Server code (from
http://tldp.org/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-ruby.html):
##!/app/ruby/1.8.x/bin/ruby
#require "xmlrpc/server"
#s = XMLRPC::Server.new(4001)
#
#class MyHandler
# def sumAndDifference(a, b)
# { "sum" => a + b, "difference" => a - b }
# end
#end
#
#s.add_handler("sample", MyHandler.new)
#s.serve
##end of server code
def call_server(sleeptime)
puts "**** Trying with sleeptime #{sleeptime} sec ****"
# Create server reference outside of loop (this works fine)
#server = XMLRPC::Client.new("127.0.0.1", "/", 4001)
for i in 1..7
# Create server reference inside of loop (this creates the problem,
when called frequently)
server = XMLRPC::Client.new("127.0.0.1", "/", 4001)
start = Time.now.tv_sec
result = server.call("sample.sumAndDifference", 5, 3)
total = Time.now.tv_sec - start
# print result and number of seconds spent during the remote call
puts "Result: #{result.inspect}. Time: #{total} sec"
# just to show we dont need to stress "that much"
# it seems that it doensnt hang when sleep >= 10 sec (on my linux
machine)
Kernel.sleep(sleeptime)
end
end
[0,5,7,10].each do |sleeptime|
call_server(sleeptime)
end
Here is the output:
================================
; ./client.rb
**** Trying with sleeptime 0 sec ****
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 30 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
**** Trying with sleeptime 5 sec ****
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 20 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 10 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
**** Trying with sleeptime 7 sec ****
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 4 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 2 sec
**** Trying with sleeptime 10 sec ****
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec
Result: {"sum"=>8, "difference"=>2}. Time: 0 sec