From: John Carter Date: 2008-10-07T12:08:14+09:00 Subject: Re: Ruby lacks atfork : The evil that lives in fork... On Tue, 7 Oct 2008, Joel VanderWerf wrote: > You can atfork in ruby; fsdb uses this kind of construct: > > module ForkSafely > def fork > # clean up before forking > super do > # clean up after forking, in child > # clean up inconsistent mutexes, etc. > yield > end > # clean up after forking, in parent > end > end > > include ForkSafely Hmm. Cute. Really cute. Can I chain the at fork handlers? Does it work for Process.fork as well? Let me try... ====================================================================== module A def fork puts "Prereal" pid = super do puts "In real" yield end puts "post real" pid end end include A pid = fork do puts "Did it work?" end Process.waitpid2 pid module B def fork puts "Prereal1" pid = super do puts "In real1" yield end puts "post real1" pid end end include B pid2 = fork do puts "Does chained work?" end Process.waitpid2 pid2 puts "Yes nested did work, does Process.fork work too?" pid3 = Process.fork do puts "Does Process.fork work?" end Process.waitpid2 pid3 puts "No it didn't" module C def Process.fork puts "b4 proc fork" super do puts "in proc fork" yield end puts "Post proc fork" end end include C pid2 = Process.fork do puts "Bah" end ====================================================================== ruby -w a.rb Prereal In real Did it work? post real Prereal1 Prereal In real In real1 Does chained work? post real post real1 Yes nested did work, does Process.fork work too? Does Process.fork work? No it didn't a.rb:52: warning: redefine fork b4 proc fork Prereal1 Prereal In real In real1 in proc fork Bah post real post real1 Post proc fork ====================================================================== Hmm. Almost, but I can't get rid of this warning... a.rb:52: warning: redefine fork any suggestions on how to get rid of that pesky warning? John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand