Posted to tcl by hypnotoad at Wed Dec 11 20:03:50 GMT 2019view raw

  1. # Master.tcl
  2. set here [file dirname [file normalize [info script]]]
  3. puts "I AM $here"
  4.  
  5. lassign [chan pipe] slaveout pipein
  6.  
  7. puts "STARTING SLAVE"
  8. set slavein [open [list | [info nameofexecutable] [file join $here slave.tcl] ] r+]
  9. set slaveout $slavein
  10.  
  11. proc SEND {command} {
  12. global slavein slaveout
  13. puts "M> $command"
  14. chan puts $slavein $command
  15. #catch {chan flush $slavein}
  16. gets $slaveout r
  17. puts "M< $r"
  18. return $r
  19. }
  20. chan configure $slaveout -buffering line
  21.  
  22. puts "SENDING COMMAND"
  23. SEND {expr 2+2}
  24. SEND {clock seconds}
  25.  
  26. after 2000
  27. SEND {expr 2+6}
  28. SEND {clock seconds}
  29. after 2000
  30. SEND {exit}
  31. exit 0
  32.  
  33. # slave.tcl
  34. set here [file dirname [file normalize [info script]]]
  35. set flog [open [file join $here log] w]
  36. chan configure $flog -buffering line
  37. puts $flog "STARTED"
  38. chan configure stdin -buffering line
  39. chan configure stdout -buffering line
  40.  
  41. while 1 {
  42. try {
  43. puts $flog "S WAIT"
  44. if {[gets stdin line]<=0} {
  45. exit 1
  46. }
  47. puts $flog "[clock seconds] S< $line"
  48. set r [eval $line]
  49. puts $flog "[clock seconds] S> $r"
  50. puts stdout $r
  51. } on error {er errdat} {
  52. puts $flog [dict get $errdat -errorinfo]
  53. }
  54. }
  55.  
  56.