Wednesday, October 5, 2011

Avoid the memory blockage during large xml request

Guys,
        When i am playing with handling the xml request through net::http discover the problem of memory blockage and just worried about how to handle that. This problem happen b'coze i have bulk of xml data in the request. So, i form the one of the solution by using the block of net http.

When we using the block of net::http. It yields each fragment of the entity body in turn as a string as it are read from the socket.

Lets we consider 2 scenario:

require 'rubygems'
require 'net/https'
require 'uri'
uri = URI.parse("http://google.com")
http = Net::HTTP.new(uri.host, uri.port)

case 1:
response = http.post('http://google.com', 'query=language')

case 2:
# using block
   File.open('test.txt',  'w') { |f|
      http.post('http://google.com', 'query=language') do |str|
          f.write str
       end
   }

In case 1 it loads the entire response in memory. While in case 2 it read each fragment and write into file. so when we use block it's avoid memory blockage.
For more information just follow

Hope above post will help you to avoid memory blockage and increase performance.

No comments:

Post a Comment