1fileevent(n)                 Tcl Built-In Commands                fileevent(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       fileevent  -  Execute  a  script  when  a  channel  becomes readable or
9       writable
10

SYNOPSIS

12       fileevent channelId readable ?script?
13
14       fileevent channelId writable ?script?
15______________________________________________________________________________
16
17

DESCRIPTION

19       This command is used to create file event handlers.  A file event  han‐
20       dler  is a binding between a channel and a script, such that the script
21       is evaluated whenever the channel becomes readable or  writable.   File
22       event handlers are most commonly used to allow data to be received from
23       another process on an event-driven basis, so that the receiver can con‐
24       tinue  to  interact with the user while waiting for the data to arrive.
25       If an application invokes gets or read on a blocking channel when there
26       is  no  input  data  available, the process will block; until the input
27       data arrives, it will not be able to service other events, so  it  will
28       appear  to  the  user  to “freeze up”.  With fileevent, the process can
29       tell when data is present and only invoke gets or read when  they  will
30       not block.
31
32       The channelId argument to fileevent refers to an open channel such as a
33       Tcl standard channel (stdin, stdout, or stderr), the return value  from
34       an  invocation  of  open or socket, or the result of a channel creation
35       command provided by a Tcl extension.
36
37       If the script argument is specified, then fileevent creates a new event
38       handler:   script  will be evaluated whenever the channel becomes read‐
39       able or writable (depending on the second argument to  fileevent).   In
40       this case fileevent returns an empty string.  The readable and writable
41       event handlers for a file are  independent,  and  may  be  created  and
42       deleted separately.  However, there may be at most one readable and one
43       writable handler for a file at a given time in a given interpreter.  If
44       fileevent  is  called  when the specified handler already exists in the
45       invoking interpreter, the new script replaces the old one.
46
47       If the script argument is not specified, fileevent returns the  current
48       script  for  channelId,  or  an  empty string if there is none.  If the
49       script argument is specified as an empty string then the event  handler
50       is deleted, so that no script will be invoked.  A file event handler is
51       also deleted automatically whenever its channel is closed or its inter‐
52       preter is deleted.
53
54       A  channel  is considered to be readable if there is unread data avail‐
55       able on the underlying device.  A channel  is  also  considered  to  be
56       readable if there is unread data in an input buffer, except in the spe‐
57       cial case where the most recent attempt to read from the channel was  a
58       gets  call  that  could  not  find a complete line in the input buffer.
59       This feature allows a file to be read a line at a time  in  nonblocking
60       mode  using  events.  A channel is also considered to be readable if an
61       end of file or error condition is present on  the  underlying  file  or
62       device.   It  is important for script to check for these conditions and
63       handle them appropriately;  for example, if there is no  special  check
64       for end of file, an infinite loop may occur where script reads no data,
65       returns, and is immediately invoked again.
66
67       A channel is considered to be writable if at least one byte of data can
68       be  written to the underlying file or device without blocking, or if an
69       error condition is present on the underlying file or device.
70
71       Event-driven I/O works best for channels that  have  been  placed  into
72       nonblocking mode with the fconfigure command.  In blocking mode, a puts
73       command may block if you give it more data than the underlying file  or
74       device can accept, and a gets or read command will block if you attempt
75       to read more data than is ready; a readable underlying file  or  device
76       may  not even guarantee that a blocking [read 1] will succeed (counter-
77       examples being multi-byte encodings, compression or  encryption  trans‐
78       forms  ). In all such cases, no events will be processed while the com‐
79       mands block.
80
81       In nonblocking mode puts, read, and gets never block.  See the documen‐
82       tation  for  the individual commands for information on how they handle
83       blocking and nonblocking channels.
84
85       Testing for the end of file condition should be done after any attempts
86       read  the channel data. The eof flag is set once an attempt to read the
87       end of data has occurred and testing before this read will  require  an
88       additional event to be fired.
89
90       The  script  for  a file event is executed at global level (outside the
91       context of any Tcl procedure) in the interpreter in which the fileevent
92       command  was  invoked.   If  an error occurs while executing the script
93       then the command registered with interp bgerror is used to  report  the
94       error.   In  addition,  the  file  event  handler is deleted if it ever
95       returns an error;  this is done in order to prevent infinite loops  due
96       to buggy handlers.
97

EXAMPLE

99       In  this  setup  GetData will be called with the channel as an argument
100       whenever $chan becomes readable.  The  read  call  will  read  whatever
101       binary  data is currently available without blocking.  Here the channel
102       has the fileevent removed when an end of file  occurs  to  avoid  being
103       continually called (see above). Alternatively the channel may be closed
104       on this condition.
105
106              proc GetData {chan} {
107                  set data [read $chan]
108                  puts "[string length $data] $data"
109                  if {[eof $chan]} {
110                      fileevent $chan readable {}
111                  }
112              }
113
114              fconfigure $chan -blocking 0 -encoding binary
115              fileevent $chan readable [list GetData $chan]
116
117       The next example demonstrates use of gets to read line-oriented data.
118
119              proc GetData {chan} {
120                  if {[gets $chan line] >= 0} {
121                      puts $line
122                  }
123                  if {[eof $chan]} {
124                      close $chan
125                  }
126              }
127
128              fconfigure $chan -blocking 0 -buffering line -translation crlf
129              fileevent $chan readable [list GetData $chan]
130

CREDITS

132       fileevent is based on the addinput command created by Mark Diekhans.
133

SEE ALSO

135       fconfigure(n), gets(n), interp(n), puts(n), read(n),  Tcl_StandardChan‐
136       nels(3)
137

KEYWORDS

139       asynchronous  I/O, blocking, channel, event handler, nonblocking, read‐
140       able, script, writable.
141
142
143
144Tcl                                   7.5                         fileevent(n)
Impressum