Seth Woolley's Man Viewer

fileevent(n) - fileevent, fileevent - Execute a script when a channel becomes readable or writable - man n fileevent

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

fileevent(n)                 Tcl Built-In Commands                fileevent(n)

       fileevent  -  Execute  a  script  when  a  channel  becomes readable or

       fileevent channelId readable ?script?

       fileevent channelId writable ?script?

       This command is used to create file(1,n) event handlers.  A file(1,n) event  han-
       dler  is a binding between a channel and a script, such that the script
       is evaluated whenever the channel becomes readable or  writable.   File
       event handlers are most commonly used to allow data to be received from
       another process on an event-driven basis, so that the receiver can con-
       tinue  to  interact with the user while waiting for the data to arrive.
       If an application invokes gets(3,n) or read(2,n,1 builtins) on a blocking channel when there
       is  no  input  data  available, the process will block; until the input
       data arrives, it will not be able to service other events, so  it  will
       appear  to  the user to ``freeze up''.  With fileevent, the process can
       tell when data is present and only invoke gets(3,n) or read(2,n,1 builtins) when they  won't

       The channelId argument to fileevent refers to an open(2,3,n) channel such as a
       Tcl standard channel (stdin, stdout, or stderr), the return value  from
       an  invocation  of  open(2,3,n) or socket(2,7,n), or the result of a channel creation
       command provided by a Tcl extension.

       If the script argument is specified, then fileevent creates a new event
       handler:   script  will be evaluated whenever the channel becomes read-
       able or writable (depending on the second argument to  fileevent).   In
       this case fileevent returns an empty string.  The readable and writable
       event handlers for a file(1,n) are  independent,  and  may  be  created  and
       deleted separately.  However, there may be at most one readable and one
       writable handler for a file(1,n) at a given time(1,2,n) in(1,8) a given interpreter.  If
       fileevent  is  called  when the specified handler already exists in(1,8) the
       invoking interpreter, the new script replaces the old one.

       If the script argument is not specified, fileevent returns the  current
       script  for  channelId,  or  an  empty string(3,n) if(3,n) there is none.  If the
       script argument is specified as an empty string(3,n) then the event  handler
       is deleted, so that no script will be invoked.  A file(1,n) event handler is
       also deleted automatically whenever its channel is closed or its inter-
       preter is deleted.

       A  channel  is considered to be readable if(3,n) there is unread data avail-
       able on the underlying device.  A channel  is  also  considered  to  be
       readable if(3,n) there is unread data in(1,8) an input buffer, except in(1,8) the spe-
       cial case where the most recent attempt to read(2,n,1 builtins) from the channel was  a
       gets(3,n)  call  that  could  not  find a complete line in(1,8) the input buffer.
       This feature allows a file(1,n) to be read(2,n,1 builtins) a line at a time(1,2,n)  in(1,8)  nonblocking
       mode  using  events.  A channel is also considered to be readable if(3,n) an
       end of file(1,n) or error(8,n) condition is present on  the  underlying  file(1,n)  or
       device.   It  is important for script to check for these conditions and
       handle them appropriately;  for example, if(3,n) there is no  special  check
       for end of file(1,n), an infinite loop may occur where script reads no data,
       returns, and is immediately invoked again.

       A channel is considered to be writable if(3,n) at least one byte of data can
       be  written to the underlying file(1,n) or device without blocking, or if(3,n) an
       error(8,n) condition is present on the underlying file(1,n) or device.

       Event-driven I/O works best for channels that  have  been  placed  into
       nonblocking mode with the fconfigure command.  In blocking mode, a puts(3,n)
       command may block if(3,n) you give it more data than the underlying file(1,n)  or
       device can accept(2,8), and a gets(3,n) or read(2,n,1 builtins) command will block if(3,n) you attempt
       to read(2,n,1 builtins) more data than is ready;  no events will be processed while the
       commands  block.  In nonblocking mode puts(3,n), read(2,n,1 builtins), and gets(3,n) never block.
       See the documentation for the individual commands  for  information  on
       how they handle blocking and nonblocking channels.

       The  script  for  a file(1,n) event is executed at global level (outside the
       context of any Tcl procedure) in(1,8) the interpreter in(1,8) which the fileevent
       command  was  invoked.   If  an error(8,n) occurs while executing the script
       then the bgerror mechanism is used to report the error.   In  addition,
       the file(1,n) event handler is deleted if(3,n) it ever returns an error(8,n);  this is
       done in(1,8) order to prevent infinite loops due to buggy handlers.

        proc(5,n) GetData {chan} {
           if(3,n) {![eof $chan]} {
               puts(3,n) [gets(3,n) $chan]

        fileevent $chan readable [list GetData $chan]

       In this setup(2,8) GetData will be called with the channel  as  an  argument
       whenever $chan becomes readable.

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

       bgerror(n), fconfigure(n), gets(3,n)(n), puts(3,n)(n), read(2,n,1 builtins)(n), Tcl_StandardChan-

       asynchronous I/O, blocking, channel, event handler, nonblocking,  read-
       able, script, writable.

Tcl                                   7.5                         fileevent(n)

References for this manual (incoming links)