fileevent
relies on the the eventloop, which you don't enter.
fileevent
just tells Tcl to call read_fifo
when it is readable.
If you want blocking IO, then just call gets
. This blocks until an entire line has been read.
set fifo [open "foo" r]
fconfigure $fifo -blocking 1
gets $fifo x
puts "x is $x"
If you do it event-driven, you need fileevent
, use non-blocking IO and you have to enter the event-loop (e.g. with vwait forever
).
set fifo [open "foo" r]
fconfigure $fifo -blocking 0
proc read_fifo {fifo} {
puts "calling read_fifo"
if {[gets $fifo x] < 0} {
if {[eof $fifo]} {
# Do some cleanup here.
close $fifo
}
}
puts "x is $x"
}
fileevent $fifo readable [list read_fifo $fifo]
vwait forever; #enter the eventloop
Don't mix event-driven with blocking IO. This does not really work.
Note that you don't have to call vwait
in Tk, doing so would reenter the event-loop, which is considered bad practice.