2014-10-20 76 views
0

要分析我想要做這樣的事情fileevent後在同一事件循環

  • 尾文件
  • 一個日誌文件中的一些時間寫分析數據後,做其他事情

這裏是我的(樣本)腳本

#!/usr/bin/tclsh 

proc readfile {fd} { 
    while {[gets $fd line] >= 0} { 
     puts $line 
    } 
} 

proc writefile {} { 
    puts xxx 
    flush stdout 
} 

if {$::argc > 0} { 
    set fd [open [list | tail --follow=name --retry [lindex $argv 0] 2>@1]] 
} else { 
    set fd stdin 
} 

after 3000 writefile 

fileevent $fd readable [list readfile $fd] 

vwait done 
close $fd 

拖尾工作正常,但腳本爲after沒有被觸發。

任何想法我做錯了什麼?

回答

1

readfile proc中,您使用的是一個while,導致它卡在其中,這就是爲什麼after未被觸發。

#!/usr/bin/tclsh 

proc readfile {fd} { 
    global done 
    puts "READ FILE CALLED..." 
    gets $fd line; # Removed 'while' loop here 
    puts "->>>>$line<<<<<<<<<" 

    ### Your condition to exit the event handler#### 
    ### set done 1; #### Changing 'done' value to 1 after that condition #### 
    ### So that the event handler will exit ####3 
} 

proc writefile {} { 
    puts "WRITE FILE CALLED" 
    puts xxx 
    flush stdout 
} 

if {$::argc > 0} { 
    set fd [open [list | tail --follow=name --retry [lindex $argv 0] 2>@1]] 
} else { 
    set fd stdin 
} 

after 3000 writefile 

fileevent $fd readable [list readfile $fd] 

vwait done 
close $fd 

輸出:

[email protected]:~/pgms/tcl$ ./ubi.tcl 
WRITE FILE CALLED 
xxx 
ubi 
READ FILE CALLED... 
->>>>ubi<<<<<<<<< 
cool 
READ FILE CALLED... 
->>>>cool<<<<<<<<< 
working 
READ FILE CALLED... 
->>>>working <<<<<<<<< 
+0

謝謝,我不知道fileevent將被稱爲每次當有可用的新生產線。如果有多行可用會發生什麼? 'readline'被多次調用,還是隻讀取一行而沒有'while'循環? – ubi 2014-10-21 10:59:41

+0

@ubi:每次都會被調用。 – Dinesh 2014-10-21 11:46:05