Hi,
I'm using an async channel to read the log from an external command.
The code skeleton is like this:
namespace eval nsc {
proc runCommand {exec_id} {
set run_${exec_id} [Parse new $exec_id]
}
oo::class create Parse {
variable chan_ans
constructor {exec_id} {
set chan_ans [open ![list {*}$comm] r]
chan configure $chan_ans -blocking 0 -buffering line
chan event $chan_ans readable [list [self object] parseLine]
}
method parseLine {} {
set status [catch {chan gets $chan_ans line} nchars]
.....
if {$status || [chan eof $chan_ans]} {
# all received
chan configure $chan_ans -blocking 1 # to assure all is written
chan event $chan_ans readable {}
chan close $chan_ans
set ::exit_flag 1
}
}
}
But the script never finishes, seems to complain at the line
'chan close $chan_ans'.
So, what should I do to correct this issue?
Hi,
I'm using an async channel to read the log from an external command.
The code skeleton is like this:
while {[llength $in_queue] > 0} {
nsc::runCommand $exec_id
vwait ::exit_flag
}
namespace eval nsc {
proc runCommand {exec_id} {
set run_${exec_id} [Parse new $exec_id]
}
oo::class create Parse {
variable chan_ans
constructor {exec_id} {
set chan_ans [open ![list {*}$comm] r]
chan configure $chan_ans -blocking 0 -buffering line
chan event $chan_ans readable [list [self object] parseLine]
}
method parseLine {} {
set status [catch {chan gets $chan_ans line} nchars]
.....
if {$status || [chan eof $chan_ans]} {
# all received
chan configure $chan_ans -blocking 1 # to assure all is written
chan event $chan_ans readable {}
chan close $chan_ans
set ::exit_flag 1
}
}
}
But the script never finishes, seems to complain at the line
'chan close $chan_ans'.
In the book "The Tcl Programming Language", by Ashok, in page 437 (466 of the PDF) there's this:
"When an end of file is seen on a channel, it is crucial to EITHER remove the read handler from the channel aw we have done, or to close the channel in the handler itself before returning.
Otherwise, the channel will continuously raise readable events because the channel is at end of file."
Are the chan event readable {} and chan close incompatible?
So, what should I do to correct this issue?
Luis Mendes <luisXXXlupeXXX@gmail.com> wrote:
Hi,And includes a syntax error (missing brace).
I'm using an async channel to read the log from an external command.
The code skeleton is like this:
namespace eval nsc {
proc runCommand {exec_id} {
set run_${exec_id} [Parse new $exec_id]
}
oo::class create Parse {
variable chan_ans
constructor {exec_id} {
set chan_ans [open ![list {*}$comm] r]
chan configure $chan_ans -blocking 0 -buffering line
chan event $chan_ans readable [list [self object] parseLine]
}
method parseLine {} {
set status [catch {chan gets $chan_ans line} nchars]
.....
if {$status || [chan eof $chan_ans]} {
# all received
chan configure $chan_ans -blocking 1 # to assure all is written
Syntax error. Tcl is not bash, you only get to insert a comment using
# where Tcl's expecting a command. So 'same line' comments have to terminate the current command first (with a ;) and then the comment can start. The above line needs to read:
chan configure $chan_ans -blocking 1 ;# to assure all is written
To avoid the syntax error. And once the syntax error is fixed, and a missing } inserted, your code works just fine.
Number two, given that you have a read only channel open in this code example, there is no need to flip to blocking, as there is no 'write
data' in a read only channel.
chan event $chan_ans readable {}
Unnecessary if you are going to also close the channel, as closing the channel takes care of removing the readable event.
chan close $chan_ans
set ::exit_flag 1
}
}
}
But the script never finishes, seems to complain at the line
'chan close $chan_ans'.
It never finishes because the syntax error from the incorrect inline
comment aborts the event call, such that no more event calls happen, so there's never another entry to your object to ultimately set the vwait variable and abort the script.
So, what should I do to correct this issue?
Fix the comment syntax error (which was all I needed to fix to make the example you posted work properly). I'm assuming the missing close
brace (}) was a copy/paste omission here.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 920 |
Nodes: | 10 (1 / 9) |
Uptime: | 88:15:43 |
Calls: | 12,188 |
Files: | 186,526 |
Messages: | 2,237,147 |