Posted to tcl by mjanssen at Wed Nov 21 10:24:04 GMT 2007view raw

  1. # A
  2. package require comm
  3.  
  4. # it behaves almost like a normal interactive session :P
  5. # invoke 'eventloop' to enter the event loop
  6. proc stdin {} {
  7. global stdin
  8. if {[info complete [append stdin [gets stdin]]]} {
  9. if {$stdin=="exit"} {
  10. unset ::eventloop; unset stdin
  11. } {
  12. catch {uplevel #0 $stdin} stdin
  13. puts -nonewline "$stdin\n% "
  14. flush stdout
  15. set stdin ""
  16. }
  17. } {
  18. append stdin "\n"
  19. }
  20. }
  21. proc eventloop {} {
  22. if {[info exists eventloop]} {
  23. puts -nonewline "You're already in the event loop :)\n%"
  24. flush stdout
  25. } {
  26. fileevent stdin readable stdin
  27. set ::eventloop 1
  28. puts -nonewline "Entering the event loop...\n% "
  29. flush stdout
  30. vwait eventloop
  31. puts "Exiting the event loop..."
  32. fileevent stdin readable {}
  33. }
  34. }
  35.  
  36. proc done {id results} {
  37. puts "$id is done -> $results"
  38. }
  39.  
  40. proc do {id cmd} {
  41. comm::comm send -async $id [list do [comm::comm self] $cmd]
  42. return {}
  43. }
  44.  
  45. eventloop
  46.  
  47.  
  48. # B
  49.  
  50. package require comm
  51. puts "I am [comm::comm id]"
  52.  
  53. proc do {from cmd} {
  54. puts "executing \"$cmd\" from $from"
  55. catch {uplevel #0 $cmd} result
  56. comm::comm send -async $from [list done [comm::comm self] $result]
  57. }
  58.  
  59. vwait forever
  60.  
  61.  
  62. # Output A
  63.  
  64. Entering the event loop...
  65. % do 2109 {expr {4+5}}
  66.  
  67. % 2109 is done -> 9
  68. # wait some time in B
  69.  
  70. % do 2109 {puts "calculating" ; after 5000 ; puts "calculation done" ; expr {4+5}}
  71.  
  72. % 2109 is done -> 9
  73.  
  74.  
  75. # Output B
  76.  
  77. I am 2109
  78. executing "expr {4+5}" from 2108
  79. executing "puts "calculating" ; after 5000 ; puts "calculation done" ; expr {4+5}" from 2108
  80. calculating
  81. calculation done