… | |
… | |
141 | =head2 I/O WATCHERS |
141 | =head2 I/O WATCHERS |
142 | |
142 | |
143 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
143 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
144 | with the following mandatory key-value pairs as arguments: |
144 | with the following mandatory key-value pairs as arguments: |
145 | |
145 | |
146 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch for |
146 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch |
147 | events. C<poll> must be a string that is either C<r> or C<w>, which |
147 | for events. C<poll> must be a string that is either C<r> or C<w>, |
148 | creates a watcher waiting for "r"eadable or "w"ritable events, |
148 | which creates a watcher waiting for "r"eadable or "w"ritable events, |
149 | respectively. C<cb> is the callback to invoke each time the file handle |
149 | respectively. C<cb> is the callback to invoke each time the file handle |
150 | becomes ready. |
150 | becomes ready. |
|
|
151 | |
|
|
152 | Although the callback might get passed parameters, their value and |
|
|
153 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
154 | callbacks cannot use arguments passed to I/O watcher callbacks. |
151 | |
155 | |
152 | The I/O watcher might use the underlying file descriptor or a copy of it. |
156 | The I/O watcher might use the underlying file descriptor or a copy of it. |
153 | You must not close a file handle as long as any watcher is active on the |
157 | You must not close a file handle as long as any watcher is active on the |
154 | underlying file descriptor. |
158 | underlying file descriptor. |
155 | |
159 | |
156 | Some event loops issue spurious readyness notifications, so you should |
160 | Some event loops issue spurious readyness notifications, so you should |
157 | always use non-blocking calls when reading/writing from/to your file |
161 | always use non-blocking calls when reading/writing from/to your file |
158 | handles. |
162 | handles. |
159 | |
|
|
160 | Although the callback might get passed parameters, their value and |
|
|
161 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
162 | callbacks cannot use arguments passed to I/O watcher callbacks. |
|
|
163 | |
163 | |
164 | Example: |
164 | Example: |
165 | |
165 | |
166 | # wait for readability of STDIN, then read a line and disable the watcher |
166 | # wait for readability of STDIN, then read a line and disable the watcher |
167 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
167 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
… | |
… | |
174 | |
174 | |
175 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
175 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
176 | method with the following mandatory arguments: |
176 | method with the following mandatory arguments: |
177 | |
177 | |
178 | C<after> specifies after how many seconds (fractional values are |
178 | C<after> specifies after how many seconds (fractional values are |
179 | supported) should the timer activate. C<cb> the callback to invoke in that |
179 | supported) the callback should be invoked. C<cb> is the callback to invoke |
180 | case. |
180 | in that case. |
|
|
181 | |
|
|
182 | Although the callback might get passed parameters, their value and |
|
|
183 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
184 | callbacks cannot use arguments passed to time watcher callbacks. |
181 | |
185 | |
182 | The timer callback will be invoked at most once: if you want a repeating |
186 | The timer callback will be invoked at most once: if you want a repeating |
183 | timer you have to create a new watcher (this is a limitation by both Tk |
187 | timer you have to create a new watcher (this is a limitation by both Tk |
184 | and Glib). |
188 | and Glib). |
185 | |
|
|
186 | Although the callback might get passed parameters, their value and |
|
|
187 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
188 | callbacks cannot use arguments passed to time watcher callbacks. |
|
|
189 | |
189 | |
190 | Example: |
190 | Example: |
191 | |
191 | |
192 | # fire an event after 7.7 seconds |
192 | # fire an event after 7.7 seconds |
193 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
193 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
… | |
… | |
234 | |
234 | |
235 | You can watch for signals using a signal watcher, C<signal> is the signal |
235 | You can watch for signals using a signal watcher, C<signal> is the signal |
236 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
236 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
237 | be invoked whenever a signal occurs. |
237 | be invoked whenever a signal occurs. |
238 | |
238 | |
|
|
239 | Although the callback might get passed parameters, their value and |
|
|
240 | presence is undefined and you cannot rely on them. Portable AnyEvent |
|
|
241 | callbacks cannot use arguments passed to signal watcher callbacks. |
|
|
242 | |
239 | Multiple signal occurances can be clumped together into one callback |
243 | Multiple signal occurances can be clumped together into one callback |
240 | invocation, and callback invocation will be synchronous. synchronous means |
244 | invocation, and callback invocation will be synchronous. synchronous means |
241 | that it might take a while until the signal gets handled by the process, |
245 | that it might take a while until the signal gets handled by the process, |
242 | but it is guarenteed not to interrupt any other callbacks. |
246 | but it is guarenteed not to interrupt any other callbacks. |
243 | |
247 | |
… | |
… | |
257 | |
261 | |
258 | The child process is specified by the C<pid> argument (if set to C<0>, it |
262 | The child process is specified by the C<pid> argument (if set to C<0>, it |
259 | watches for any child process exit). The watcher will trigger as often |
263 | watches for any child process exit). The watcher will trigger as often |
260 | as status change for the child are received. This works by installing a |
264 | as status change for the child are received. This works by installing a |
261 | signal handler for C<SIGCHLD>. The callback will be called with the pid |
265 | signal handler for C<SIGCHLD>. The callback will be called with the pid |
262 | and exit status (as returned by waitpid). |
266 | and exit status (as returned by waitpid), so unlike other watcher types, |
|
|
267 | you I<can> rely on child watcher callback arguments. |
263 | |
268 | |
264 | There is a slight catch to child watchers, however: you usually start them |
269 | There is a slight catch to child watchers, however: you usually start them |
265 | I<after> the child process was created, and this means the process could |
270 | I<after> the child process was created, and this means the process could |
266 | have exited already (and no SIGCHLD will be sent anymore). |
271 | have exited already (and no SIGCHLD will be sent anymore). |
267 | |
272 | |