【问题标题】:Tailing a local file using fileevent使用 fileevent 拖尾本地文件
【发布时间】:2017-11-08 07:17:15
【问题描述】:

我正在尝试使用 fileevent 跟踪文件(应该只有 Tcl 8.4 版本,所以我不能使用 chan 命令)。

% proc GetData {chan} {
 set data [read $chan]
    puts "[string length $data] $data"
    if {[eof $chan]} {
        fileevent $chan readable {}
    }
}
% 
% 
% 
%  set fp [open "|tail -f /home/dinesh/input" r+]
file7
% fconfigure $fp
-blocking 1 -buffering full -buffersize 4096 -encoding utf-8 -eofchar {{} {}} -translation {auto lf}
% fconfigure $fp -blocking 0  -buffering  line 
% 
% 
% 
% 
%  fileevent $fp readable [list GetData $fp]
% 

我已经在另一个终端中使用cat 命令更改了文件内容,但仍然没有调用GetData

使用gets $fp line,我可以得到修改后的内容,但是为什么没有触发fileevent?

【问题讨论】:

    标签: file events tcl readable


    【解决方案1】:

    使用fileevent 安排的回调仅在您运行事件循环时发生,默认情况下tclsh 不会为您运行事件循环。尝试这样做,看看我的假设是否正确:

    update
    

    如果是,您需要将程序设计为由回调驱动。运行事件循环的标准方式是这样的:

    vwait forever
    # Any global variable name would do, but “forever” has a nice feel
    

    但这对您的交互式测试帮助不大。 (解决这个问题的一种方法是加载 Tk 包;默认情况下会打开运行事件循环,因此您不需要明确这样做。并为您提供管理窗口,因此并非没有后果……)

    【讨论】:

      猜你喜欢
      • 2014-10-29
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      相关资源
      最近更新 更多