Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/fileevent.n @ 37

Last change on this file since 37 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 5.4 KB
Line 
1'\"
2'\" Copyright (c) 1994 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\"
5'\" See the file "license.terms" for information on usage and redistribution
6'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7'\"
8'\" RCS: @(#) $Id: fileevent.n,v 1.13 2007/12/13 15:22:32 dgp Exp $
9'\"
10.so man.macros
11.TH fileevent n 7.5 Tcl "Tcl Built-In Commands"
12.BS
13'\" Note:  do not modify the .SH NAME line immediately below!
14.SH NAME
15fileevent \- Execute a script when a channel becomes readable or writable
16.SH SYNOPSIS
17\fBfileevent \fIchannelId \fBreadable \fR?\fIscript\fR?
18.sp
19\fBfileevent \fIchannelId \fBwritable \fR?\fIscript\fR?
20.BE
21
22.SH DESCRIPTION
23.PP
24This command is used to create \fIfile event handlers\fR.  A file event
25handler is a binding between a channel and a script, such that the script
26is evaluated whenever the channel becomes readable or writable.  File event
27handlers are most commonly used to allow data to be received from another
28process on an event-driven basis, so that the receiver can continue to
29interact with the user while waiting for the data to arrive.  If an
30application invokes \fBgets\fR or \fBread\fR on a blocking channel when
31there is no input data available, the process will block; until the input
32data arrives, it will not be able to service other events, so it will
33appear to the user to
34.QW "freeze up" .
35With \fBfileevent\fR, the process can
36tell when data is present and only invoke \fBgets\fR or \fBread\fR when
37they will not block.
38.PP
39The \fIchannelId\fR argument to \fBfileevent\fR refers to an open
40channel such as a Tcl standard channel (\fBstdin\fR, \fBstdout\fR,
41or \fBstderr\fR), the return value from an invocation of \fBopen\fR
42or \fBsocket\fR, or the result of a channel creation command provided
43by a Tcl extension.
44.PP
45If the \fIscript\fR argument is specified, then \fBfileevent\fR
46creates a new event handler:  \fIscript\fR will be evaluated
47whenever the channel becomes readable or writable (depending on the
48second argument to \fBfileevent\fR).
49In this case \fBfileevent\fR returns an empty string.
50The \fBreadable\fR and \fBwritable\fR event handlers for a file
51are independent, and may be created and deleted separately.
52However, there may be at most one \fBreadable\fR and one \fBwritable\fR
53handler for a file at a given time in a given interpreter.
54If \fBfileevent\fR is called when the specified handler already
55exists in the invoking interpreter, the new script replaces the old one.
56.PP
57If the \fIscript\fR argument is not specified, \fBfileevent\fR
58returns the current script for \fIchannelId\fR, or an empty string
59if there is none.
60If the \fIscript\fR argument is specified as an empty string
61then the event handler is deleted, so that no script will be invoked.
62A file event handler is also deleted automatically whenever
63its channel is closed or its interpreter is deleted.
64.PP
65A channel is considered to be readable if there is unread data
66available on the underlying device.
67A channel is also considered to be readable if there is unread
68data in an input buffer, except in the special case where the
69most recent attempt to read from the channel was a \fBgets\fR
70call that could not find a complete line in the input buffer.
71This feature allows a file to be read a line at a time in nonblocking mode
72using events.
73A channel is also considered to be readable if an end of file or
74error condition is present on the underlying file or device.
75It is important for \fIscript\fR to check for these conditions
76and handle them appropriately;  for example, if there is no special
77check for end of file, an infinite loop may occur where \fIscript\fR
78reads no data, returns, and is immediately invoked again.
79.PP
80A channel is considered to be writable if at least one byte of data
81can be written to the underlying file or device without blocking,
82or if an error condition is present on the underlying file or device.
83.PP
84Event-driven I/O works best for channels that have been
85placed into nonblocking mode with the \fBfconfigure\fR command.
86In blocking mode, a \fBputs\fR command may block if you give it
87more data than the underlying file or device can accept, and a
88\fBgets\fR or \fBread\fR command will block if you attempt to read
89more data than is ready;  no events will be processed while the
90commands block.
91In nonblocking mode \fBputs\fR, \fBread\fR, and \fBgets\fR never block.
92See the documentation for the individual commands for information
93on how they handle blocking and nonblocking channels.
94.PP
95The script for a file event is executed at global level (outside the
96context of any Tcl procedure) in the interpreter in which the
97\fBfileevent\fR command was invoked.
98If an error occurs while executing the script then the
99command registered with \fBinterp bgerror\fR is used to report the error.
100In addition, the file event handler is deleted if it ever returns
101an error;  this is done in order to prevent infinite loops due to
102buggy handlers.
103.SH EXAMPLE
104In this setup \fBGetData\fR will be called with the channel as an
105argument whenever $chan becomes readable.
106.CS
107proc GetData {chan} {
108    if {![eof $chan]} {
109        puts [gets $chan]
110    }
111}
112
113\fBfileevent\fR $chan readable [list GetData $chan]
114.CE
115
116.SH CREDITS
117.PP
118\fBfileevent\fR is based on the \fBaddinput\fR command created
119by Mark Diekhans.
120
121.SH "SEE ALSO"
122fconfigure(n), gets(n), interp(n), puts(n), read(n), Tcl_StandardChannels(3)
123
124.SH KEYWORDS
125asynchronous I/O, blocking, channel, event handler, nonblocking, readable,
126script, writable.
Note: See TracBrowser for help on using the repository browser.