Posted to tcl by mjanssen at Wed Nov 21 10:24:04 GMT 2007view raw
- # A
- package require comm
- # it behaves almost like a normal interactive session :P
- # invoke 'eventloop' to enter the event loop
- proc stdin {} {
- global stdin
- if {[info complete [append stdin [gets stdin]]]} {
- if {$stdin=="exit"} {
- unset ::eventloop; unset stdin
- } {
- catch {uplevel #0 $stdin} stdin
- puts -nonewline "$stdin\n% "
- flush stdout
- set stdin ""
- }
- } {
- append stdin "\n"
- }
- }
- proc eventloop {} {
- if {[info exists eventloop]} {
- puts -nonewline "You're already in the event loop :)\n%"
- flush stdout
- } {
- fileevent stdin readable stdin
- set ::eventloop 1
- puts -nonewline "Entering the event loop...\n% "
- flush stdout
- vwait eventloop
- puts "Exiting the event loop..."
- fileevent stdin readable {}
- }
- }
- proc done {id results} {
- puts "$id is done -> $results"
- }
- proc do {id cmd} {
- comm::comm send -async $id [list do [comm::comm self] $cmd]
- return {}
- }
- eventloop
- # B
- package require comm
- puts "I am [comm::comm id]"
- proc do {from cmd} {
- puts "executing \"$cmd\" from $from"
- catch {uplevel #0 $cmd} result
- comm::comm send -async $from [list done [comm::comm self] $result]
- }
- vwait forever
- # Output A
- Entering the event loop...
- % do 2109 {expr {4+5}}
- % 2109 is done -> 9
- # wait some time in B
- % do 2109 {puts "calculating" ; after 5000 ; puts "calculation done" ; expr {4+5}}
- % 2109 is done -> 9
- # Output B
- I am 2109
- executing "expr {4+5}" from 2108
- executing "puts "calculating" ; after 5000 ; puts "calculation done" ; expr {4+5}" from 2108
- calculating
- calculation done