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