ruby: multiple identical or synced instances of mechanize?
As far as I know, I read elsewhere that ruby mechanize is not thread save. Thus, to accelerate some 'gets', I opted to instantiate several independent Mechanize objects and use them in parallel. This seems to work OK
BTW, I would like to make all instances as similar as possible, as similar as sharing 'everything' they could know (cookies, etc).
Is开发者_Go百科 there any way to make deep copies of an already 'configured' Mechanize object. My aim is to only configure one of them and copy make clones of it.
For instance, if I can create a Mechanize object like this (only an example, but suppose there are a lot more of configured attributes):
agent = Mechanize.new { |a| a.read_timeout = 20; a.max_history = 1 }
How can I get copies of that don't interfere each other while 'get'ing?.
agent2 = agent.dup # are not thread save copies
agent2 = Marshal.load(Marshal.dump(agent)) # thorws an error
This appears to work until you change a value for max_history or read_timeout.
class Mechanize
def clone
Mechanize.new do |a|
a.cookie_jar = cookie_jar
a.max_history = max_history
a.read_timeout = read_timeout
end
end
end
Testing:
agent1 = Mechanize.new { |a| a.max_history = 30; a.read_timeout = 30 }
agent2 = agent1.clone
agent2.max_history == 30 # true
agent2.cookie_jar == agent1.cookie_jar # true
精彩评论