Un problema è necessario utilizzare Process.wait
per attendere il completamento dei processi fork. L'altro è che non puoi fare la comunicazione interprocesso attraverso le variabili. Per vedere questo:
@one = nil
@two = nil
@hash = {}
pidA = fork do
sleep 1
@one = 1
@hash[:one] = 1
p [:one, @one, :hash, @hash] #=> [ :one, 1, :hash, { :one => 1 } ]
end
pidB = fork do
sleep 2
@two = 2
@hash[:two] = 2
p [:two, @two, :hash, @hash] #=> [ :two, 2, :hash, { :two => 2 } ]
end
Process.wait(pidB)
Process.wait(pidA)
p [:one, @one, :two, @two, :hash, @hash] #=> [ :one, nil, :two, nil, :hash, {} ]
Un modo per farlo comunicazione tra processi sta usando un tubo (IO::pipe
). Aprilo prima di forchetta, quindi fai in modo che ogni lato della forcella chiuda un'estremità del tubo.
Da ri IO::pipe
:
rd, wr = IO.pipe
if fork
wr.close
puts "Parent got: <#{rd.read}>"
rd.close
Process.wait
else
rd.close
puts "Sending message to parent"
wr.write "Hi Dad"
wr.close
end
_produces:_
Sending message to parent
Parent got: <Hi Dad>
Se si desidera condividere variabili, utilizzare le discussioni:
@one = nil
@two = nil
@hash = {}
threadA = Thread.fork do
sleep 1
@one = 1
@hash[:one] = 1
p [:one, @one, :hash, @hash] #=> [ :one, 1, :hash, { :one => 1 } ] # (usually)
end
threadB = Thread.fork do
sleep 2
@two = 2
@hash[:two] = 2
p [:two, @two, :hash, @hash] #=> [ :two, 2, :hash, { :one => 1, :two => 2 } ] # (usually)
end
threadA.join
threadB.join
p [:one, @one, :two, @two, :hash, @hash] #=> [ :one, 1, :two, 2, :hash, { :one => 1, :two => 2 } ]
Tuttavia, non sono sicuro se filettatura ti porterà alcun guadagno quando sei IO limite.
Sei sicuro che sia YAML a prendersi il tempo? Se è così, hai provato a caricarlo con Psych piuttosto che con Syck? –