… | |
… | |
37 | # CHILD/PID STATUS CHANGES |
37 | # CHILD/PID STATUS CHANGES |
38 | |
38 | |
39 | my $w = EV::child 666, sub { |
39 | my $w = EV::child 666, sub { |
40 | my ($w, $revents) = @_; |
40 | my ($w, $revents) = @_; |
41 | my $status = $w->rstatus; |
41 | my $status = $w->rstatus; |
|
|
42 | }; |
|
|
43 | |
|
|
44 | # STAT CHANGES |
|
|
45 | my $w = EV::stat "/etc/passwd", 10, sub { |
|
|
46 | my ($w, $revents) = @_; |
|
|
47 | warn $w->path, " has changed somehow.\n"; |
42 | }; |
48 | }; |
43 | |
49 | |
44 | # MAINLOOP |
50 | # MAINLOOP |
45 | EV::loop; # loop until EV::unloop is called or all watchers stop |
51 | EV::loop; # loop until EV::unloop is called or all watchers stop |
46 | EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled |
52 | EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled |
… | |
… | |
111 | |
117 | |
112 | EV::once doesn't return anything: the watchers stay active till |
118 | EV::once doesn't return anything: the watchers stay active till |
113 | either of them triggers, then they will be stopped and freed, and |
119 | either of them triggers, then they will be stopped and freed, and |
114 | the callback invoked. |
120 | the callback invoked. |
115 | |
121 | |
116 | WATCHER |
122 | WATCHER OBJECTS |
117 | A watcher is an object that gets created to record your interest in some |
123 | A watcher is an object that gets created to record your interest in some |
118 | event. For instance, if you want to wait for STDIN to become readable, |
124 | event. For instance, if you want to wait for STDIN to become readable, |
119 | you would create an EV::io watcher for that: |
125 | you would create an EV::io watcher for that: |
120 | |
126 | |
121 | my $watcher = EV::io *STDIN, EV::READ, sub { |
127 | my $watcher = EV::io *STDIN, EV::READ, sub { |
… | |
… | |
145 | |
151 | |
146 | Also, all methods changing some aspect of a watcher (->set, ->priority, |
152 | Also, all methods changing some aspect of a watcher (->set, ->priority, |
147 | ->fh and so on) automatically stop and start it again if it is active, |
153 | ->fh and so on) automatically stop and start it again if it is active, |
148 | which means pending events get lost. |
154 | which means pending events get lost. |
149 | |
155 | |
150 | WATCHER TYPES |
156 | COMMON WATCHER METHODS |
151 | Now lets move to the existing watcher types and asociated methods. |
157 | This section lists methods common to all watchers. |
152 | |
|
|
153 | The following methods are available for all watchers. Then followes a |
|
|
154 | description of each watcher constructor (EV::io, EV::timer, |
|
|
155 | EV::periodic, EV::signal, EV::child, EV::idle, EV::prepare and |
|
|
156 | EV::check), followed by any type-specific methods (if any). |
|
|
157 | |
158 | |
158 | $w->start |
159 | $w->start |
159 | Starts a watcher if it isn't active already. Does nothing to an |
160 | Starts a watcher if it isn't active already. Does nothing to an |
160 | already active watcher. By default, all watchers start out in the |
161 | already active watcher. By default, all watchers start out in the |
161 | active state (see the description of the "_ns" variants if you need |
162 | active state (see the description of the "_ns" variants if you need |
… | |
… | |
229 | |
230 | |
230 | my $udp_socket = ... |
231 | my $udp_socket = ... |
231 | my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... }; |
232 | my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... }; |
232 | $udp_watcher->keepalive (0); |
233 | $udp_watcher->keepalive (0); |
233 | |
234 | |
|
|
235 | WATCHER TYPES |
|
|
236 | Each of the following subsections describes a single watcher type. |
|
|
237 | |
|
|
238 | IO WATCHERS - is this file descriptor readable or writable? |
234 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
239 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
235 | $w = EV::io_ns $fileno_or_fh, $eventmask, $callback |
240 | $w = EV::io_ns $fileno_or_fh, $eventmask, $callback |
236 | As long as the returned watcher object is alive, call the $callback |
241 | As long as the returned watcher object is alive, call the $callback |
237 | when the events specified in $eventmask. |
242 | when at least one of events specified in $eventmask occurs. |
238 | |
243 | |
239 | The $eventmask can be one or more of these constants ORed together: |
244 | The $eventmask can be one or more of these constants ORed together: |
240 | |
245 | |
241 | EV::READ wait until read() wouldn't block anymore |
246 | EV::READ wait until read() wouldn't block anymore |
242 | EV::WRITE wait until write() wouldn't block anymore |
247 | EV::WRITE wait until write() wouldn't block anymore |
… | |
… | |
254 | |
259 | |
255 | $current_eventmask = $w->events |
260 | $current_eventmask = $w->events |
256 | $old_eventmask = $w->events ($new_eventmask) |
261 | $old_eventmask = $w->events ($new_eventmask) |
257 | Returns the previously set event mask and optionally set a new one. |
262 | Returns the previously set event mask and optionally set a new one. |
258 | |
263 | |
|
|
264 | TIMER WATCHERS - relative and optionally repeating timeouts |
259 | $w = EV::timer $after, $repeat, $callback |
265 | $w = EV::timer $after, $repeat, $callback |
260 | $w = EV::timer_ns $after, $repeat, $callback |
266 | $w = EV::timer_ns $after, $repeat, $callback |
261 | Calls the callback after $after seconds (which may be fractional). |
267 | Calls the callback after $after seconds (which may be fractional). |
262 | If $repeat is non-zero, the timer will be restarted (with the |
268 | If $repeat is non-zero, the timer will be restarted (with the |
263 | $repeat value as $after) after the callback returns. |
269 | $repeat value as $after) after the callback returns. |
… | |
… | |
277 | The "timer_ns" variant doesn't start (activate) the newly created |
283 | The "timer_ns" variant doesn't start (activate) the newly created |
278 | watcher. |
284 | watcher. |
279 | |
285 | |
280 | $w->set ($after, $repeat) |
286 | $w->set ($after, $repeat) |
281 | Reconfigures the watcher, see the constructor above for details. Can |
287 | Reconfigures the watcher, see the constructor above for details. Can |
282 | be at any time. |
288 | be called at any time. |
283 | |
289 | |
284 | $w->again |
290 | $w->again |
285 | Similar to the "start" method, but has special semantics for |
291 | Similar to the "start" method, but has special semantics for |
286 | repeating timers: |
292 | repeating timers: |
287 | |
293 | |
… | |
… | |
298 | This behaviour is useful when you have a timeout for some IO |
304 | This behaviour is useful when you have a timeout for some IO |
299 | operation. You create a timer object with the same value for $after |
305 | operation. You create a timer object with the same value for $after |
300 | and $repeat, and then, in the read/write watcher, run the "again" |
306 | and $repeat, and then, in the read/write watcher, run the "again" |
301 | method on the timeout. |
307 | method on the timeout. |
302 | |
308 | |
|
|
309 | PERIODIC WATCHERS - to cron or not to cron? |
303 | $w = EV::periodic $at, $interval, $reschedule_cb, $callback |
310 | $w = EV::periodic $at, $interval, $reschedule_cb, $callback |
304 | $w = EV::periodic_ns $at, $interval, $reschedule_cb, $callback |
311 | $w = EV::periodic_ns $at, $interval, $reschedule_cb, $callback |
305 | Similar to EV::timer, but is not based on relative timeouts but on |
312 | Similar to EV::timer, but is not based on relative timeouts but on |
306 | absolute times. Apart from creating "simple" timers that trigger |
313 | absolute times. Apart from creating "simple" timers that trigger |
307 | "at" the specified time, it can also be used for non-drifting |
314 | "at" the specified time, it can also be used for non-drifting |
… | |
… | |
372 | The "periodic_ns" variant doesn't start (activate) the newly created |
379 | The "periodic_ns" variant doesn't start (activate) the newly created |
373 | watcher. |
380 | watcher. |
374 | |
381 | |
375 | $w->set ($at, $interval, $reschedule_cb) |
382 | $w->set ($at, $interval, $reschedule_cb) |
376 | Reconfigures the watcher, see the constructor above for details. Can |
383 | Reconfigures the watcher, see the constructor above for details. Can |
377 | be at any time. |
384 | be called at any time. |
378 | |
385 | |
379 | $w->again |
386 | $w->again |
380 | Simply stops and starts the watcher again. |
387 | Simply stops and starts the watcher again. |
381 | |
388 | |
|
|
389 | SIGNAL WATCHERS - signal me when a signal gets signalled! |
382 | $w = EV::signal $signal, $callback |
390 | $w = EV::signal $signal, $callback |
383 | $w = EV::signal_ns $signal, $callback |
391 | $w = EV::signal_ns $signal, $callback |
384 | Call the callback when $signal is received (the signal can be |
392 | Call the callback when $signal is received (the signal can be |
385 | specified by number or by name, just as with kill or %SIG). |
393 | specified by number or by name, just as with "kill" or %SIG). |
386 | |
394 | |
387 | EV will grab the signal for the process (the kernel only allows one |
395 | EV will grab the signal for the process (the kernel only allows one |
388 | component to receive a signal at a time) when you start a signal |
396 | component to receive a signal at a time) when you start a signal |
389 | watcher, and removes it again when you stop it. Perl does the same |
397 | watcher, and removes it again when you stop it. Perl does the same |
390 | when you add/remove callbacks to %SIG, so watch out. |
398 | when you add/remove callbacks to %SIG, so watch out. |
… | |
… | |
394 | The "signal_ns" variant doesn't start (activate) the newly created |
402 | The "signal_ns" variant doesn't start (activate) the newly created |
395 | watcher. |
403 | watcher. |
396 | |
404 | |
397 | $w->set ($signal) |
405 | $w->set ($signal) |
398 | Reconfigures the watcher, see the constructor above for details. Can |
406 | Reconfigures the watcher, see the constructor above for details. Can |
399 | be at any time. |
407 | be called at any time. |
400 | |
408 | |
401 | $current_signum = $w->signal |
409 | $current_signum = $w->signal |
402 | $old_signum = $w->signal ($new_signal) |
410 | $old_signum = $w->signal ($new_signal) |
403 | Returns the previously set signal (always as a number not name) and |
411 | Returns the previously set signal (always as a number not name) and |
404 | optionally set a new one. |
412 | optionally set a new one. |
405 | |
413 | |
|
|
414 | CHILD WATCHERS - watch out for process status changes |
406 | $w = EV::child $pid, $callback |
415 | $w = EV::child $pid, $callback |
407 | $w = EV::child_ns $pid, $callback |
416 | $w = EV::child_ns $pid, $callback |
408 | Call the callback when a status change for pid $pid (or any pid if |
417 | Call the callback when a status change for pid $pid (or any pid if |
409 | $pid is 0) has been received. More precisely: when the process |
418 | $pid is 0) has been received. More precisely: when the process |
410 | receives a SIGCHLD, EV will fetch the outstanding exit/wait status |
419 | receives a "SIGCHLD", EV will fetch the outstanding exit/wait status |
411 | for all changed/zombie children and call the callback. |
420 | for all changed/zombie children and call the callback. |
412 | |
421 | |
413 | You can access both status and pid by using the "rstatus" and "rpid" |
422 | It is valid (and fully supported) to install a child watcher after a |
|
|
423 | child has exited but before the event loop has started its next |
|
|
424 | iteration (for example, first you "fork", then the new child process |
|
|
425 | might exit, and only then do you install a child watcher in the |
|
|
426 | parent for the new pid). |
|
|
427 | |
|
|
428 | You can access both exit (or tracing) status and pid by using the |
414 | methods on the watcher object. |
429 | "rstatus" and "rpid" methods on the watcher object. |
415 | |
430 | |
416 | You can have as many pid watchers per pid as you want. |
431 | You can have as many pid watchers per pid as you want, they will all |
|
|
432 | be called. |
417 | |
433 | |
418 | The "child_ns" variant doesn't start (activate) the newly created |
434 | The "child_ns" variant doesn't start (activate) the newly created |
419 | watcher. |
435 | watcher. |
420 | |
436 | |
421 | $w->set ($pid) |
437 | $w->set ($pid) |
422 | Reconfigures the watcher, see the constructor above for details. Can |
438 | Reconfigures the watcher, see the constructor above for details. Can |
423 | be at any time. |
439 | be called at any time. |
424 | |
440 | |
425 | $current_pid = $w->pid |
441 | $current_pid = $w->pid |
426 | $old_pid = $w->pid ($new_pid) |
442 | $old_pid = $w->pid ($new_pid) |
427 | Returns the previously set process id and optionally set a new one. |
443 | Returns the previously set process id and optionally set a new one. |
428 | |
444 | |
… | |
… | |
432 | |
448 | |
433 | $pid = $w->rpid |
449 | $pid = $w->rpid |
434 | Return the pid of the awaited child (useful when you have installed |
450 | Return the pid of the awaited child (useful when you have installed |
435 | a watcher for all pids). |
451 | a watcher for all pids). |
436 | |
452 | |
|
|
453 | STAT WATCHERS - did the file attributes just change? |
|
|
454 | $w = EV::stat $path, $interval, $callback |
|
|
455 | $w = EV::stat_ns $path, $interval, $callback |
|
|
456 | Call the callback when a file status change has been detected on |
|
|
457 | $path. The $path does not need to exist, changing from "path exists" |
|
|
458 | to "path does not exist" is a status change like any other. |
|
|
459 | |
|
|
460 | The $interval is a recommended polling interval for systems where |
|
|
461 | OS-supported change notifications don't exist or are not supported. |
|
|
462 | If you use 0 then an unspecified default is used (which is highly |
|
|
463 | recommended!), which is to be expected to be around five seconds |
|
|
464 | usually. |
|
|
465 | |
|
|
466 | This watcher type is not meant for massive numbers of stat watchers, |
|
|
467 | as even with OS-supported change notifications, this can be |
|
|
468 | resource-intensive. |
|
|
469 | |
|
|
470 | The "stat_ns" variant doesn't start (activate) the newly created |
|
|
471 | watcher. |
|
|
472 | |
|
|
473 | ... = $w->stat |
|
|
474 | This call is very similar to the perl "stat" built-in: It stats |
|
|
475 | (using "lstat") the path specified in the watcher and sets perls |
|
|
476 | stat cache (as well as EV's idea of the current stat values) to the |
|
|
477 | values found. |
|
|
478 | |
|
|
479 | In scalar context, a boolean is return indicating success or failure |
|
|
480 | of the stat. In list context, the same 13-value list as with stat is |
|
|
481 | returned (except that the blksize and blocks fields are not |
|
|
482 | reliable). |
|
|
483 | |
|
|
484 | In the case of an error, errno is set to "ENOENT" (regardless of the |
|
|
485 | actual error value) and the "nlink" value is forced to zero (if the |
|
|
486 | stat was successful then nlink is guaranteed to be non-zero). |
|
|
487 | |
|
|
488 | See also the next two entries for more info. |
|
|
489 | |
|
|
490 | ... = $w->attr |
|
|
491 | Just like "$w->stat", but without the initial stat'ing: this returns |
|
|
492 | the values most recently detected by EV. See the next entry for more |
|
|
493 | info. |
|
|
494 | |
|
|
495 | ... = $w->prev |
|
|
496 | Just like "$w->stat", but without the initial stat'ing: this returns |
|
|
497 | the previous set of values, before the change. |
|
|
498 | |
|
|
499 | That is, when the watcher callback is invoked, "$w->prev" will be |
|
|
500 | set to the values found *before* a change was detected, while |
|
|
501 | "$w->attr" returns the values found leading to the change detection. |
|
|
502 | The difference (if any) between "prev" and "attr" is what triggered |
|
|
503 | the callback. |
|
|
504 | |
|
|
505 | If you did something to the filesystem object and do not want to |
|
|
506 | trigger yet another change, you can call "stat" to update EV's idea |
|
|
507 | of what the current attributes are. |
|
|
508 | |
|
|
509 | $w->set ($path, $interval) |
|
|
510 | Reconfigures the watcher, see the constructor above for details. Can |
|
|
511 | be called at any time. |
|
|
512 | |
|
|
513 | $current_path = $w->path |
|
|
514 | $old_path = $w->path ($new_path) |
|
|
515 | Returns the previously set path and optionally set a new one. |
|
|
516 | |
|
|
517 | $current_interval = $w->interval |
|
|
518 | $old_interval = $w->interval ($new_interval) |
|
|
519 | Returns the previously set interval and optionally set a new one. |
|
|
520 | Can be used to query the actual interval used. |
|
|
521 | |
|
|
522 | IDLE WATCHERS - when you've got nothing better to do... |
437 | $w = EV::idle $callback |
523 | $w = EV::idle $callback |
438 | $w = EV::idle_ns $callback |
524 | $w = EV::idle_ns $callback |
439 | Call the callback when there are no pending io, timer/periodic, |
525 | Call the callback when there are no pending io, timer/periodic, |
440 | signal or child events, i.e. when the process is idle. |
526 | signal or child events, i.e. when the process is idle. |
441 | |
527 | |
… | |
… | |
443 | and they will be called repeatedly until stopped. |
529 | and they will be called repeatedly until stopped. |
444 | |
530 | |
445 | The "idle_ns" variant doesn't start (activate) the newly created |
531 | The "idle_ns" variant doesn't start (activate) the newly created |
446 | watcher. |
532 | watcher. |
447 | |
533 | |
|
|
534 | PREPARE WATCHERS - customise your event loop! |
448 | $w = EV::prepare $callback |
535 | $w = EV::prepare $callback |
449 | $w = EV::prepare_ns $callback |
536 | $w = EV::prepare_ns $callback |
450 | Call the callback just before the process would block. You can still |
537 | Call the callback just before the process would block. You can still |
451 | create/modify any watchers at this point. |
538 | create/modify any watchers at this point. |
452 | |
539 | |
453 | See the EV::check watcher, below, for explanations and an example. |
540 | See the EV::check watcher, below, for explanations and an example. |
454 | |
541 | |
455 | The "prepare_ns" variant doesn't start (activate) the newly created |
542 | The "prepare_ns" variant doesn't start (activate) the newly created |
456 | watcher. |
543 | watcher. |
457 | |
544 | |
|
|
545 | CHECK WATCHERS - customise your event loop even more! |
458 | $w = EV::check $callback |
546 | $w = EV::check $callback |
459 | $w = EV::check_ns $callback |
547 | $w = EV::check_ns $callback |
460 | Call the callback just after the process wakes up again (after it |
548 | Call the callback just after the process wakes up again (after it |
461 | has gathered events), but before any other callbacks have been |
549 | has gathered events), but before any other callbacks have been |
462 | invoked. |
550 | invoked. |
… | |
… | |
504 | The callbacks of the created watchers will not be called as the |
592 | The callbacks of the created watchers will not be called as the |
505 | watchers are destroyed before this cna happen (remember EV::check |
593 | watchers are destroyed before this cna happen (remember EV::check |
506 | gets called first). |
594 | gets called first). |
507 | |
595 | |
508 | The "check_ns" variant doesn't start (activate) the newly created |
596 | The "check_ns" variant doesn't start (activate) the newly created |
|
|
597 | watcher. |
|
|
598 | |
|
|
599 | FORK WATCHERS - the audacity to resume the event loop after a fork |
|
|
600 | Fork watchers are called when a "fork ()" was detected. The invocation |
|
|
601 | is done before the event loop blocks next and before "check" watchers |
|
|
602 | are being called, and only in the child after the fork. |
|
|
603 | |
|
|
604 | $w = EV::fork $callback |
|
|
605 | $w = EV::fork_ns $callback |
|
|
606 | Call the callback before the event loop is resumed in the child |
|
|
607 | process after a fork. |
|
|
608 | |
|
|
609 | The "fork_ns" variant doesn't start (activate) the newly created |
509 | watcher. |
610 | watcher. |
510 | |
611 | |
511 | THREADS |
612 | THREADS |
512 | Threads are not supported by this module in any way. Perl pseudo-threads |
613 | Threads are not supported by this module in any way. Perl pseudo-threads |
513 | is evil stuff and must die. As soon as Perl gains real threads I will |
614 | is evil stuff and must die. As soon as Perl gains real threads I will |
… | |
… | |
529 | |
630 | |
530 | On win32, there is no notion of fork so all this doesn't apply, of |
631 | On win32, there is no notion of fork so all this doesn't apply, of |
531 | course. |
632 | course. |
532 | |
633 | |
533 | SEE ALSO |
634 | SEE ALSO |
534 | L<EV::DNS>. |
635 | EV::DNS. |
535 | |
636 | |
536 | AUTHOR |
637 | AUTHOR |
537 | Marc Lehmann <schmorp@schmorp.de> |
638 | Marc Lehmann <schmorp@schmorp.de> |
538 | http://home.schmorp.de/ |
639 | http://home.schmorp.de/ |
539 | |
640 | |