… | |
… | |
15 | warn "is called roughly every 2s (repeat = 1)"; |
15 | warn "is called roughly every 2s (repeat = 1)"; |
16 | }; |
16 | }; |
17 | |
17 | |
18 | undef $w; # destroy event watcher again |
18 | undef $w; # destroy event watcher again |
19 | |
19 | |
20 | my $w = EV::periodic 0, 60, sub { |
20 | my $w = EV::periodic 0, 60, 0, sub { |
21 | warn "is called every minute, on the minute, exactly"; |
21 | warn "is called every minute, on the minute, exactly"; |
22 | }; |
22 | }; |
23 | |
23 | |
24 | # IO |
24 | # IO |
25 | |
25 | |
… | |
… | |
39 | }; |
39 | }; |
40 | |
40 | |
41 | # CHILD/PID STATUS CHANGES |
41 | # CHILD/PID STATUS CHANGES |
42 | |
42 | |
43 | my $w = EV::child 666, sub { |
43 | my $w = EV::child 666, sub { |
44 | my ($w, $revents, $status) = @_; |
44 | my ($w, $revents) = @_; |
|
|
45 | # my $pid = $w->rpid; |
|
|
46 | my $status = $w->rstatus; |
45 | }; |
47 | }; |
46 | |
48 | |
47 | # MAINLOOP |
49 | # MAINLOOP |
48 | EV::loop; # loop until EV::loop_done is called |
50 | EV::loop; # loop until EV::loop_done is called |
49 | EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled |
51 | EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled |
… | |
… | |
115 | In the rare case where one wants to create a watcher but not start it at |
117 | In the rare case where one wants to create a watcher but not start it at |
116 | the same time, each constructor has a variant with a trailing "_ns" in |
118 | the same time, each constructor has a variant with a trailing "_ns" in |
117 | its name, e.g. EV::io has a non-starting variant EV::io_ns and so on. |
119 | its name, e.g. EV::io has a non-starting variant EV::io_ns and so on. |
118 | |
120 | |
119 | Please note that a watcher will automatically be stopped when the |
121 | Please note that a watcher will automatically be stopped when the |
120 | watcher object is returned, so you *need* to keep the watcher objects |
122 | watcher object is destroyed, so you *need* to keep the watcher objects |
121 | returned by the constructors. |
123 | returned by the constructors. |
|
|
124 | |
|
|
125 | Also, all methods changing some aspect of a watcher (->set, ->priority, |
|
|
126 | ->fh and so on) automatically stop and start it again if it is active, |
|
|
127 | which means pending events get lost. |
122 | |
128 | |
123 | WATCHER TYPES |
129 | WATCHER TYPES |
124 | Now lets move to the existing watcher types and asociated methods. |
130 | Now lets move to the existing watcher types and asociated methods. |
125 | |
131 | |
126 | The following methods are available for all watchers. Then followes a |
132 | The following methods are available for all watchers. Then followes a |
… | |
… | |
141 | not. |
147 | not. |
142 | |
148 | |
143 | $bool = $w->is_active |
149 | $bool = $w->is_active |
144 | Returns true if the watcher is active, false otherwise. |
150 | Returns true if the watcher is active, false otherwise. |
145 | |
151 | |
|
|
152 | $current_data = $w->data |
|
|
153 | $old_data = $w->data ($new_data) |
|
|
154 | Queries a freely usable data scalar on the watcher and optionally |
|
|
155 | changes it. This is a way to associate custom data with a watcher: |
|
|
156 | |
|
|
157 | my $w = EV::timer 60, 0, sub { |
|
|
158 | warn $_[0]->data; |
|
|
159 | }; |
|
|
160 | $w->data ("print me!"); |
|
|
161 | |
146 | $current_cb = $w->cb |
162 | $current_cb = $w->cb |
147 | $old_cb = $w->cb ($new_cb) |
163 | $old_cb = $w->cb ($new_cb) |
148 | Queries the callback on the watcher and optionally changes it. You |
164 | Queries the callback on the watcher and optionally changes it. You |
149 | cna do this at any time. |
165 | can do this at any time without the watcher restarting. |
|
|
166 | |
|
|
167 | $current_priority = $w->priority |
|
|
168 | $old_priority = $w->priority ($new_priority) |
|
|
169 | Queries the priority on the watcher and optionally changes it. |
|
|
170 | Pending watchers with higher priority will be invoked first. The |
|
|
171 | valid range of priorities lies between EV::MAXPRI (default 2) and |
|
|
172 | EV::MINPRI (default -2). If the priority is outside this range it |
|
|
173 | will automatically be normalised to the nearest valid priority. |
|
|
174 | |
|
|
175 | The default priority of any newly-created weatcher is 0. |
150 | |
176 | |
151 | $w->trigger ($revents) |
177 | $w->trigger ($revents) |
152 | Call the callback *now* with the given event mask. |
178 | Call the callback *now* with the given event mask. |
153 | |
179 | |
154 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
180 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
… | |
… | |
215 | This behaviour is useful when you have a timeout for some IO |
241 | This behaviour is useful when you have a timeout for some IO |
216 | operation. You create a timer object with the same value for $after |
242 | operation. You create a timer object with the same value for $after |
217 | and $repeat, and then, in the read/write watcher, run the "again" |
243 | and $repeat, and then, in the read/write watcher, run the "again" |
218 | method on the timeout. |
244 | method on the timeout. |
219 | |
245 | |
220 | $w = EV::periodic $at, $interval, $callback |
246 | $w = EV::periodic $at, $interval, $reschedule_cb, $callback |
221 | $w = EV::periodic_ns $at, $interval, $callback |
247 | $w = EV::periodic_ns $at, $interval, $reschedule_cb, $callback |
222 | Similar to EV::timer, but the time is given as an absolute point in |
248 | Similar to EV::timer, but is not based on relative timeouts but on |
223 | time ($at), plus an optional $interval. |
249 | absolute times. Apart from creating "simple" timers that trigger |
|
|
250 | "at" the specified time, it can also be used for non-drifting |
|
|
251 | absolute timers and more complex, cron-like, setups that are not |
|
|
252 | adversely affected by time jumps (i.e. when the system clock is |
|
|
253 | changed by explicit date -s or other means such as ntpd). It is also |
|
|
254 | the most complex watcher type in EV. |
224 | |
255 | |
225 | If the $interval is zero, then the callback will be called at the |
256 | It has three distinct "modes": |
226 | time $at if that is in the future, or as soon as possible if it is |
|
|
227 | in the past. It will not automatically repeat. |
|
|
228 | |
257 | |
229 | If the $interval is nonzero, then the watcher will always be |
258 | * absolute timer ($interval = $reschedule_cb = 0) |
230 | scheduled to time out at the next "$at + N * $interval" time. |
259 | This time simply fires at the wallclock time $at and doesn't |
|
|
260 | repeat. It will not adjust when a time jump occurs, that is, if |
|
|
261 | it is to be run at January 1st 2011 then it will run when the |
|
|
262 | system time reaches or surpasses this time. |
231 | |
263 | |
232 | This can be used to schedule a callback to run at very regular |
264 | * non-repeating interval timer ($interval > 0, $reschedule_cb = 0) |
233 | intervals, as long as the processing time is less then the interval |
265 | In this mode the watcher will always be scheduled to time out at |
234 | (otherwise obviously events will be skipped). |
266 | the next "$at + N * $interval" time (for some integer N) and |
|
|
267 | then repeat, regardless of any time jumps. |
235 | |
268 | |
|
|
269 | This can be used to create timers that do not drift with respect |
|
|
270 | to system time: |
|
|
271 | |
|
|
272 | my $hourly = EV::periodic 0, 3600, 0, sub { print "once/hour\n" }; |
|
|
273 | |
|
|
274 | That doesn't mean there will always be 3600 seconds in between |
|
|
275 | triggers, but only that the the clalback will be called when the |
|
|
276 | system time shows a full hour (UTC). |
|
|
277 | |
236 | Another way to think about it (for the mathematically inclined) is |
278 | Another way to think about it (for the mathematically inclined) |
237 | that EV::periodic will try to run the callback at the next possible |
279 | is that EV::periodic will try to run the callback in this mode |
238 | time where "$time = $at (mod $interval)", regardless of any time |
280 | at the next possible time where "$time = $at (mod $interval)", |
239 | jumps. |
281 | regardless of any time jumps. |
240 | |
282 | |
241 | This periodic timer is based on "wallclock time", that is, if the |
283 | * manual reschedule mode ($reschedule_cb = coderef) |
242 | clock changes ("ntp", "date -s" etc.), then the timer will |
284 | In this mode $interval and $at are both being ignored. Instead, |
243 | nevertheless run at the specified time. This means it will never |
285 | each time the periodic watcher gets scheduled, the first |
244 | drift (it might jitter, but it will not drift). |
286 | callback ($reschedule_cb) will be called with the watcher as |
|
|
287 | first, and the current time as second argument. |
|
|
288 | |
|
|
289 | *This callback MUST NOT stop or destroy this or any other |
|
|
290 | periodic watcher, ever*. If you need to stop it, return 1e30 and |
|
|
291 | stop it afterwards. |
|
|
292 | |
|
|
293 | It must return the next time to trigger, based on the passed |
|
|
294 | time value (that is, the lowest time value larger than to the |
|
|
295 | second argument). It will usually be called just before the |
|
|
296 | callback will be triggered, but might be called at other times, |
|
|
297 | too. |
|
|
298 | |
|
|
299 | This can be used to create very complex timers, such as a timer |
|
|
300 | that triggers on each midnight, local time (actually 24 hours |
|
|
301 | after the last midnight, to keep the example simple. If you know |
|
|
302 | a way to do it correctly in about the same space (without |
|
|
303 | requiring elaborate modules), drop me a note :): |
|
|
304 | |
|
|
305 | my $daily = EV::periodic 0, 0, sub { |
|
|
306 | my ($w, $now) = @_; |
|
|
307 | |
|
|
308 | use Time::Local (); |
|
|
309 | my (undef, undef, undef, $d, $m, $y) = localtime $now; |
|
|
310 | 86400 + Time::Local::timelocal 0, 0, 0, $d, $m, $y |
|
|
311 | }, sub { |
|
|
312 | print "it's midnight or likely shortly after, now\n"; |
|
|
313 | }; |
245 | |
314 | |
246 | The "periodic_ns" variant doesn't start (activate) the newly created |
315 | The "periodic_ns" variant doesn't start (activate) the newly created |
247 | watcher. |
316 | watcher. |
248 | |
317 | |
249 | $w->set ($at, $interval) |
318 | $w->set ($at, $interval, $reschedule_cb) |
250 | Reconfigures the watcher, see the constructor above for details. Can |
319 | Reconfigures the watcher, see the constructor above for details. Can |
251 | be at any time. |
320 | be at any time. |
|
|
321 | |
|
|
322 | $w->again |
|
|
323 | Simply stops and starts the watcher again. |
252 | |
324 | |
253 | $w = EV::signal $signal, $callback |
325 | $w = EV::signal $signal, $callback |
254 | $w = EV::signal_ns $signal, $callback |
326 | $w = EV::signal_ns $signal, $callback |
255 | Call the callback when $signal is received (the signal can be |
327 | Call the callback when $signal is received (the signal can be |
256 | specified by number or by name, just as with kill or %SIG). |
328 | specified by number or by name, just as with kill or %SIG). |
… | |
… | |
266 | watcher. |
338 | watcher. |
267 | |
339 | |
268 | $w->set ($signal) |
340 | $w->set ($signal) |
269 | Reconfigures the watcher, see the constructor above for details. Can |
341 | Reconfigures the watcher, see the constructor above for details. Can |
270 | be at any time. |
342 | be at any time. |
|
|
343 | |
|
|
344 | $current_signum = $w->signal |
|
|
345 | $old_signum = $w->signal ($new_signal) |
|
|
346 | Returns the previously set signal (always as a number not name) and |
|
|
347 | optionally set a new one. |
271 | |
348 | |
272 | $w = EV::child $pid, $callback |
349 | $w = EV::child $pid, $callback |
273 | $w = EV::child_ns $pid, $callback |
350 | $w = EV::child_ns $pid, $callback |
274 | Call the callback when a status change for pid $pid (or any pid if |
351 | Call the callback when a status change for pid $pid (or any pid if |
275 | $pid is 0) has been received. More precisely: when the process |
352 | $pid is 0) has been received. More precisely: when the process |
276 | receives a SIGCHLD, EV will fetch the outstanding exit/wait status |
353 | receives a SIGCHLD, EV will fetch the outstanding exit/wait status |
277 | for all changed/zombie children and call the callback. |
354 | for all changed/zombie children and call the callback. |
278 | |
355 | |
279 | Unlike all other callbacks, this callback will be called with an |
356 | You can access both status and pid by using the "rstatus" and "rpid" |
280 | additional third argument which is the exit status. See the |
357 | methods on the watcher object. |
281 | "waitpid" function for details. |
|
|
282 | |
358 | |
283 | You can have as many pid watchers per pid as you want. |
359 | You can have as many pid watchers per pid as you want. |
284 | |
360 | |
285 | The "child_ns" variant doesn't start (activate) the newly created |
361 | The "child_ns" variant doesn't start (activate) the newly created |
286 | watcher. |
362 | watcher. |
287 | |
363 | |
288 | $w->set ($pid) |
364 | $w->set ($pid) |
289 | Reconfigures the watcher, see the constructor above for details. Can |
365 | Reconfigures the watcher, see the constructor above for details. Can |
290 | be at any time. |
366 | be at any time. |
|
|
367 | |
|
|
368 | $current_pid = $w->pid |
|
|
369 | $old_pid = $w->pid ($new_pid) |
|
|
370 | Returns the previously set process id and optionally set a new one. |
|
|
371 | |
|
|
372 | $exit_status = $w->rstatus |
|
|
373 | Return the exit/wait status (as returned by waitpid, see the waitpid |
|
|
374 | entry in perlfunc). |
|
|
375 | |
|
|
376 | $pid = $w->rpid |
|
|
377 | Return the pid of the awaited child (useful when you have installed |
|
|
378 | a watcher for all pids). |
291 | |
379 | |
292 | $w = EV::idle $callback |
380 | $w = EV::idle $callback |
293 | $w = EV::idle_ns $callback |
381 | $w = EV::idle_ns $callback |
294 | Call the callback when there are no pending io, timer/periodic, |
382 | Call the callback when there are no pending io, timer/periodic, |
295 | signal or child events, i.e. when the process is idle. |
383 | signal or child events, i.e. when the process is idle. |