Returning data from forked processes

后端 未结 7 869
别那么骄傲
别那么骄傲 2020-12-08 14:37

If I do

Process.fork do 
  x 
end 

how can I know what x returned (e.g. true/fase/string) ?

(Writing to a file/database is not an

7条回答
  •  既然无缘
    2020-12-08 15:30

    Yes, you can create a subprocess to execute a block inside.

    I recommend the aw gem:

    Aw.fork! { 6 * 7 } # => 42
    

    Of course, it prevents from side effects:

    arr = ['foo']
    Aw.fork! { arr << 'FUU' } # => ["foo", "FUU"]
    arr # => ["foo"]
    

提交回复
热议问题