📄 fileevent.n
字号:
'\"'\" Copyright (c) 1994 The Regents of the University of California.'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" SCCS: @(#) fileevent.n 1.6 96/02/23 13:46:29'\" .so man.macros.TH fileevent n 7.5 Tcl "Tcl Built-In Commands".BS'\" Note: do not modify the .SH NAME line immediately below!.SH NAMEfileevent \- Execute a script when a channel becomes readable or writable.SH SYNOPSIS\fBfileevent \fIchannelId \fBreadable \fR?\fIscript\fR?.sp\fBfileevent \fIchannelId \fBwritable \fR?\fIscript\fR?.BE.SH DESCRIPTION.PPThis command is used to create \fIfile event handlers\fR. A file eventhandler is a binding between a channel and a script, such that the scriptis evaluated whenever the channel becomes readable or writable. File eventhandlers are most commonly used to allow data to be received from anotherprocess on an event-driven basis, so that the receiver can continue tointeract with the user while waiting for the data to arrive. If anapplication invokes \fBgets\fR or \fBread\fR on a blocking channel whenthere is no input data available, the process will block; until the inputdata arrives, it will not be able to service other events, so it willappear to the user to ``freeze up''. With \fBfileevent\fR, the process cantell when data is present and only invoke \fBgets\fR or \fBread\fR whenthey won't block..PPThe \fIchannelId\fR argument to \fBfileevent\fR refers to an open channel,such as the return value from a previous \fBopen\fR or \fBsocket\fRcommand.If the \fIscript\fR argument is specified, then \fBfileevent\fRcreates a new event handler: \fIscript\fR will be evaluatedwhenever the channel becomes readable or writable (depending on thesecond argument to \fBfileevent\fR).In this case \fBfileevent\fR returns an empty string.The \fBreadable\fR and \fBwritable\fR event handlers for a fileare independent, and may be created and deleted separately.However, there may be at most one \fBreadable\fR and one \fBwritable\fRhandler for a file at a given time in a given interpreter.If \fBfileevent\fR is called when the specified handler alreadyexists in the invoking interpreter, the new script replaces the old one..PPIf the \fIscript\fR argument is not specified, \fBfileevent\fRreturns the current script for \fIchannelId\fR, or an empty stringif there is none.If the \fIscript\fR argument is specified as an empty stringthen the event handler is deleted, so that no script will be invoked.A file event handler is also deleted automatically wheneverits channel is closed or its interpreter is deleted..PPA channel is considered to be readable if there is unread dataavailable on the underlying device.A channel is also considered to be readable if there is unreaddata in an input buffer, except in the special case where themost recent attempt to read from the channel was a \fBgets\fRcall that could not find a complete line in the input buffer.This feature allows a file to be read a line at a time in nonblocking modeusing events.A channel is also considered to be readable if an end of file orerror condition is present on the underlying file or device.It is important for \fIscript\fR to check for these conditionsand handle them appropriately; for example, if there is no specialcheck for end of file, an infinite loop may occur where \fIscript\fRreads no data, returns, and is immediately invoked again..PPA channel is considered to be writable if at least one byte of datacan be written to the underlying file or device without blocking,or if an error condition is present on the underlying file or device..PPEvent-driven I/O works best for channels that have beenplaced into nonblocking mode with the \fBfconfigure\fR command.In blocking mode, a \fBputs\fR command may block if you give itmore data than the underlying file or device can accept, and a\fBgets\fR or \fBread\fR command will block if you attempt to readmore data than is ready; no events will be processed while thecommands block.In nonblocking mode \fBputs\fR, \fBread\fR, and \fBgets\fR never block.See the documentation for the individual commands for informationon how they handle blocking and nonblocking channels..PPThe script for a file event is executed at global level (outside thecontext of any Tcl procedure) in the interpreter in which the\fBfileevent\fR command was invoked.If an error occurs while executing the script then the\fBbgerror\fR mechanism is used to report the error.In addition, the file event handler is deleted if it ever returnsan error; this is done in order to prevent infinite loops due tobuggy handlers..SH CREDITS.PP\fBfileevent\fR is based on the \fBaddinput\fR command createdby Mark Diekhans..SH "SEE ALSO"bgerror, fconfigure, gets, puts, read.SH KEYWORDSasynchronous I/O, blocking, channel, event handler, nonblocking, readable,script, writable.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -