Posted to tcl by schelte at Tue Jun 15 21:54:46 GMT 2010view raw

  1. a.tcl:
  2. #!/usr/bin/tclsh
  3.  
  4. set f [open "|./b.tcl" r+]
  5. fconfigure $f -buffering line -blocking 0
  6. fileevent $f readable [list data $f]
  7.  
  8. proc data {fd} {
  9. if {[eof $fd]} {
  10. puts EOF
  11. close $fd
  12. exit
  13. } elseif {[gets $fd line] != -1} {
  14. puts $line
  15. }
  16. }
  17.  
  18. after 12000 puts $f reset
  19. after 32000 puts $f quit
  20.  
  21. vwait forever
  22.  
  23. ###############################################################
  24.  
  25. b.tcl:
  26. #!/usr/bin/tclsh
  27.  
  28. fileevent stdin readable [list data stdin]
  29. fconfigure stdin -blocking 0
  30. fconfigure stdout -buffering line
  31.  
  32. proc data {fd} {
  33. if {[eof $fd]} {
  34. exit
  35. } elseif {[gets $fd line]} {
  36. if {$line eq "quit"} {
  37. exit
  38. } elseif {$line eq "reset"} {
  39. set ::counter 0
  40. }
  41. }
  42. }
  43.  
  44. proc output {} {
  45. puts [incr ::counter]
  46. after 5000 output
  47. }
  48.  
  49. output
  50. vwait forever
  51.