From: Robert Klemme Date: 2012-06-29T23:54:13+09:00 Subject: Re: Asynchronous IO --14dae934114bfe4ab604c39d9e8a Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Thu, Jun 28, 2012 at 4:36 PM, Andrea Dallera wrote: > =A0This is far from a clean solution, mind you: Things to improve: - use the block form of IO.popen - close writing when finished - join on the reader thread > =A0 =A0 pipe =3D=A0IO.popen("command", "options") > =A0 =A0 Thread.new do > =A0 =A0 =A0 while(true) > =A0 =A0 =A0 =A0 p pipe.gets > =A0 =A0 =A0 end > =A0 =A0 end > =A0 =A0 pipe.puts("I'm writing this on the pipe") > > You should see the process's output in your STDOUT. $ ruby19 popen.rb 0.000 main : "started" 0.040 writer : "about to write first line" 0.041 reader : " 1\tfirst line\n" 1.041 writer : "about to write second line" 1.041 reader : " 2\tsecond line\n" 6.041 writer : "about to write last line" 6.041 reader : " 3\tlast line\n" 6.041 writer : "closed" 6.041 writer : "joined" 6.042 main : "finished" Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --14dae934114bfe4ab604c39d9e8a Content-Type: application/octet-stream; name="popen.rb" Content-Disposition: attachment; filename="popen.rb" Content-Transfer-Encoding: base64 X-Attachment-Id: f_h41dtobg0 JGJhc2UgPSBUaW1lLm5vdwoKZGVmIGxvZyhsYWJlbCwgbXNnKQogIHByaW50ZiAiJTcuM2YgJS0x MHM6ICVwXG4iLCBUaW1lLm5vdyAtICRiYXNlLCBsYWJlbCwgbXNnCmVuZAoKbG9nICdtYWluJywg J3N0YXJ0ZWQnCgpJTy5wb3BlbigiY2F0IC1uIiwgInIrIikgZG8gfGlvfAogIHRoID0gVGhyZWFk Lm5ldyBkbwogICAgaW8uZWFjaF9saW5lIGRvIHxsaW5lfAogICAgICBsb2cgJ3JlYWRlcicsIGxp bmUKICAgIGVuZAogIGVuZAoKICBsb2cgJ3dyaXRlcicsICdhYm91dCB0byB3cml0ZSBmaXJzdCBs aW5lJwogIGlvLnB1dHMgImZpcnN0IGxpbmUiCiAgc2xlZXAgMQogIGxvZyAnd3JpdGVyJywgJ2Fi b3V0IHRvIHdyaXRlIHNlY29uZCBsaW5lJwogIGlvLnB1dHMgInNlY29uZCBsaW5lIgogIHNsZWVw IDUKICBsb2cgJ3dyaXRlcicsICdhYm91dCB0byB3cml0ZSBsYXN0IGxpbmUnCiAgaW8ucHV0cyAi bGFzdCBsaW5lIgogIGlvLmNsb3NlX3dyaXRlCiAgbG9nICd3cml0ZXInLCAnY2xvc2VkJwoKICB0 aC5qb2luCiAgbG9nICd3cml0ZXInLCAnam9pbmVkJwplbmQKCmxvZyAnbWFpbicsICdmaW5pc2hl ZCcKCg== --14dae934114bfe4ab604c39d9e8a--