… | |
… | |
56 | This module provides an interface to libev |
56 | This module provides an interface to libev |
57 | (<http://software.schmorp.de/pkg/libev.html>). While the documentation |
57 | (<http://software.schmorp.de/pkg/libev.html>). While the documentation |
58 | below is comprehensive, one might also consult the documentation of |
58 | below is comprehensive, one might also consult the documentation of |
59 | libev itself (<http://cvs.schmorp.de/libev/ev.html>) for more subtle |
59 | libev itself (<http://cvs.schmorp.de/libev/ev.html>) for more subtle |
60 | details on watcher semantics or some discussion on the available |
60 | details on watcher semantics or some discussion on the available |
61 | backends, or how to force a specific backend with "LIBEV_FLAGS". |
61 | backends, or how to force a specific backend with "LIBEV_FLAGS", or just |
|
|
62 | about in any case because it has much more detailed information. |
62 | |
63 | |
63 | BASIC INTERFACE |
64 | BASIC INTERFACE |
64 | $EV::DIED |
65 | $EV::DIED |
65 | Must contain a reference to a function that is called when a |
66 | Must contain a reference to a function that is called when a |
66 | callback throws an exception (with $@ containing thr error). The |
67 | callback throws an exception (with $@ containing the error). The |
67 | default prints an informative message and continues. |
68 | default prints an informative message and continues. |
68 | |
69 | |
69 | If this callback throws an exception it will be silently ignored. |
70 | If this callback throws an exception it will be silently ignored. |
70 | |
71 | |
71 | $time = EV::time |
72 | $time = EV::time |
… | |
… | |
94 | When called with no arguments or an argument of EV::UNLOOP_ONE, |
95 | When called with no arguments or an argument of EV::UNLOOP_ONE, |
95 | makes the innermost call to EV::loop return. |
96 | makes the innermost call to EV::loop return. |
96 | |
97 | |
97 | When called with an argument of EV::UNLOOP_ALL, all calls to |
98 | When called with an argument of EV::UNLOOP_ALL, all calls to |
98 | EV::loop will return as fast as possible. |
99 | EV::loop will return as fast as possible. |
|
|
100 | |
|
|
101 | $count = EV::loop_count |
|
|
102 | Return the number of times the event loop has polled for new events. |
|
|
103 | Sometiems useful as a generation counter. |
99 | |
104 | |
100 | EV::once $fh_or_undef, $events, $timeout, $cb->($revents) |
105 | EV::once $fh_or_undef, $events, $timeout, $cb->($revents) |
101 | This function rolls together an I/O and a timer watcher for a single |
106 | This function rolls together an I/O and a timer watcher for a single |
102 | one-shot event without the need for managing a watcher object. |
107 | one-shot event without the need for managing a watcher object. |
103 | |
108 | |
… | |
… | |
117 | |
122 | |
118 | EV::once doesn't return anything: the watchers stay active till |
123 | EV::once doesn't return anything: the watchers stay active till |
119 | either of them triggers, then they will be stopped and freed, and |
124 | either of them triggers, then they will be stopped and freed, and |
120 | the callback invoked. |
125 | the callback invoked. |
121 | |
126 | |
|
|
127 | EV::feed_fd_event ($fd, $revents) |
|
|
128 | Feed an event on a file descriptor into EV. EV will react to this |
|
|
129 | call as if the readyness notifications specified by $revents (a |
|
|
130 | combination of "EV::READ" and "EV::WRITE") happened on the file |
|
|
131 | descriptor $fd. |
|
|
132 | |
|
|
133 | EV::feed_signal_event ($signal) |
|
|
134 | Feed a signal event into EV. EV will react to this call as if the |
|
|
135 | signal specified by $signal had occured. |
|
|
136 | |
122 | WATCHER OBJECTS |
137 | WATCHER OBJECTS |
123 | A watcher is an object that gets created to record your interest in some |
138 | A watcher is an object that gets created to record your interest in some |
124 | event. For instance, if you want to wait for STDIN to become readable, |
139 | event. For instance, if you want to wait for STDIN to become readable, |
125 | you would create an EV::io watcher for that: |
140 | you would create an EV::io watcher for that: |
126 | |
141 | |
… | |
… | |
135 | received events. |
150 | received events. |
136 | |
151 | |
137 | Each watcher type has its associated bit in revents, so you can use the |
152 | Each watcher type has its associated bit in revents, so you can use the |
138 | same callback for multiple watchers. The event mask is named after the |
153 | same callback for multiple watchers. The event mask is named after the |
139 | type, i..e. EV::child sets EV::CHILD, EV::prepare sets EV::PREPARE, |
154 | type, i..e. EV::child sets EV::CHILD, EV::prepare sets EV::PREPARE, |
140 | EV::periodic sets EV::PERIODIC and so on, with the exception of IO |
155 | EV::periodic sets EV::PERIODIC and so on, with the exception of I/O |
141 | events (which can set both EV::READ and EV::WRITE bits), and EV::timer |
156 | events (which can set both EV::READ and EV::WRITE bits), and EV::timer |
142 | (which uses EV::TIMEOUT). |
157 | (which uses EV::TIMEOUT). |
143 | |
158 | |
144 | In the rare case where one wants to create a watcher but not start it at |
159 | In the rare case where one wants to create a watcher but not start it at |
145 | the same time, each constructor has a variant with a trailing "_ns" in |
160 | the same time, each constructor has a variant with a trailing "_ns" in |
… | |
… | |
163 | stopped watchers). |
178 | stopped watchers). |
164 | |
179 | |
165 | $w->stop |
180 | $w->stop |
166 | Stop a watcher if it is active. Also clear any pending events |
181 | Stop a watcher if it is active. Also clear any pending events |
167 | (events that have been received but that didn't yet result in a |
182 | (events that have been received but that didn't yet result in a |
168 | callback invocation), regardless of wether the watcher was active or |
183 | callback invocation), regardless of whether the watcher was active |
169 | not. |
184 | or not. |
170 | |
185 | |
171 | $bool = $w->is_active |
186 | $bool = $w->is_active |
172 | Returns true if the watcher is active, false otherwise. |
187 | Returns true if the watcher is active, false otherwise. |
173 | |
188 | |
174 | $current_data = $w->data |
189 | $current_data = $w->data |
… | |
… | |
197 | The default priority of any newly-created watcher is 0. |
212 | The default priority of any newly-created watcher is 0. |
198 | |
213 | |
199 | Note that the priority semantics have not yet been fleshed out and |
214 | Note that the priority semantics have not yet been fleshed out and |
200 | are subject to almost certain change. |
215 | are subject to almost certain change. |
201 | |
216 | |
202 | $w->trigger ($revents) |
217 | $w->invoke ($revents) |
203 | Call the callback *now* with the given event mask. |
218 | Call the callback *now* with the given event mask. |
|
|
219 | |
|
|
220 | $w->feed_event ($revents) |
|
|
221 | Feed some events on this watcher into EV. EV will react to this call |
|
|
222 | as if the watcher had received the given $revents mask. |
|
|
223 | |
|
|
224 | $revents = $w->clear_pending |
|
|
225 | If the watcher is pending, this function returns clears its pending |
|
|
226 | status and returns its $revents bitset (as if its callback was |
|
|
227 | invoked). If the watcher isn't pending it does nothing and returns |
|
|
228 | 0. |
204 | |
229 | |
205 | $previous_state = $w->keepalive ($bool) |
230 | $previous_state = $w->keepalive ($bool) |
206 | Normally, "EV::loop" will return when there are no active watchers |
231 | Normally, "EV::loop" will return when there are no active watchers |
207 | (which is a "deadlock" because no progress can be made anymore). |
232 | (which is a "deadlock" because no progress can be made anymore). |
208 | This is convinient because it allows you to start your watchers (and |
233 | This is convinient because it allows you to start your watchers (and |
… | |
… | |
223 | returning. |
248 | returning. |
224 | |
249 | |
225 | The initial value for keepalive is true (enabled), and you cna |
250 | The initial value for keepalive is true (enabled), and you cna |
226 | change it any time. |
251 | change it any time. |
227 | |
252 | |
228 | Example: Register an IO watcher for some UDP socket but do not keep |
253 | Example: Register an I/O watcher for some UDP socket but do not keep |
229 | the event loop from running just because of that watcher. |
254 | the event loop from running just because of that watcher. |
230 | |
255 | |
231 | my $udp_socket = ... |
256 | my $udp_socket = ... |
232 | my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... }; |
257 | my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... }; |
233 | $udp_watcher->keepalive (0); |
258 | $udp_watcher->keepalive (0); |
234 | |
259 | |
235 | WATCHER TYPES |
260 | WATCHER TYPES |
236 | Each of the following subsections describes a single watcher type. |
261 | Each of the following subsections describes a single watcher type. |
237 | |
262 | |
238 | IO WATCHERS - is this file descriptor readable or writable? |
263 | I/O WATCHERS - is this file descriptor readable or writable? |
239 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
264 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
240 | $w = EV::io_ns $fileno_or_fh, $eventmask, $callback |
265 | $w = EV::io_ns $fileno_or_fh, $eventmask, $callback |
241 | As long as the returned watcher object is alive, call the $callback |
266 | As long as the returned watcher object is alive, call the $callback |
242 | when at least one of events specified in $eventmask occurs. |
267 | when at least one of events specified in $eventmask occurs. |
243 | |
268 | |
… | |
… | |
384 | be called at any time. |
409 | be called at any time. |
385 | |
410 | |
386 | $w->again |
411 | $w->again |
387 | Simply stops and starts the watcher again. |
412 | Simply stops and starts the watcher again. |
388 | |
413 | |
|
|
414 | $time = $w->at |
|
|
415 | Return the time that the watcher is expected to trigger next. |
|
|
416 | |
389 | SIGNAL WATCHERS - signal me when a signal gets signalled! |
417 | SIGNAL WATCHERS - signal me when a signal gets signalled! |
390 | $w = EV::signal $signal, $callback |
418 | $w = EV::signal $signal, $callback |
391 | $w = EV::signal_ns $signal, $callback |
419 | $w = EV::signal_ns $signal, $callback |
392 | Call the callback when $signal is received (the signal can be |
420 | Call the callback when $signal is received (the signal can be |
393 | specified by number or by name, just as with "kill" or %SIG). |
421 | specified by number or by name, just as with "kill" or %SIG). |
… | |
… | |
468 | resource-intensive. |
496 | resource-intensive. |
469 | |
497 | |
470 | The "stat_ns" variant doesn't start (activate) the newly created |
498 | The "stat_ns" variant doesn't start (activate) the newly created |
471 | watcher. |
499 | watcher. |
472 | |
500 | |
|
|
501 | ... = $w->stat |
|
|
502 | This call is very similar to the perl "stat" built-in: It stats |
|
|
503 | (using "lstat") the path specified in the watcher and sets perls |
|
|
504 | stat cache (as well as EV's idea of the current stat values) to the |
|
|
505 | values found. |
|
|
506 | |
|
|
507 | In scalar context, a boolean is return indicating success or failure |
|
|
508 | of the stat. In list context, the same 13-value list as with stat is |
|
|
509 | returned (except that the blksize and blocks fields are not |
|
|
510 | reliable). |
|
|
511 | |
|
|
512 | In the case of an error, errno is set to "ENOENT" (regardless of the |
|
|
513 | actual error value) and the "nlink" value is forced to zero (if the |
|
|
514 | stat was successful then nlink is guaranteed to be non-zero). |
|
|
515 | |
|
|
516 | See also the next two entries for more info. |
|
|
517 | |
|
|
518 | ... = $w->attr |
|
|
519 | Just like "$w->stat", but without the initial stat'ing: this returns |
|
|
520 | the values most recently detected by EV. See the next entry for more |
|
|
521 | info. |
|
|
522 | |
|
|
523 | ... = $w->prev |
|
|
524 | Just like "$w->stat", but without the initial stat'ing: this returns |
|
|
525 | the previous set of values, before the change. |
|
|
526 | |
|
|
527 | That is, when the watcher callback is invoked, "$w->prev" will be |
|
|
528 | set to the values found *before* a change was detected, while |
|
|
529 | "$w->attr" returns the values found leading to the change detection. |
|
|
530 | The difference (if any) between "prev" and "attr" is what triggered |
|
|
531 | the callback. |
|
|
532 | |
|
|
533 | If you did something to the filesystem object and do not want to |
|
|
534 | trigger yet another change, you can call "stat" to update EV's idea |
|
|
535 | of what the current attributes are. |
|
|
536 | |
473 | $w->set ($path, $interval) |
537 | $w->set ($path, $interval) |
474 | Reconfigures the watcher, see the constructor above for details. Can |
538 | Reconfigures the watcher, see the constructor above for details. Can |
475 | be called at any time. |
539 | be called at any time. |
476 | |
540 | |
477 | $current_path = $w->path |
541 | $current_path = $w->path |
… | |
… | |
484 | Can be used to query the actual interval used. |
548 | Can be used to query the actual interval used. |
485 | |
549 | |
486 | IDLE WATCHERS - when you've got nothing better to do... |
550 | IDLE WATCHERS - when you've got nothing better to do... |
487 | $w = EV::idle $callback |
551 | $w = EV::idle $callback |
488 | $w = EV::idle_ns $callback |
552 | $w = EV::idle_ns $callback |
489 | Call the callback when there are no pending io, timer/periodic, |
553 | Call the callback when there are no other pending watchers of the |
490 | signal or child events, i.e. when the process is idle. |
554 | same or higher priority (excluding check, prepare and other idle |
|
|
555 | watchers of the same or lower priority, of course). They are called |
|
|
556 | idle watchers because when the watcher is the highest priority |
|
|
557 | pending event in the process, the process is considered to be idle |
|
|
558 | at that priority. |
|
|
559 | |
|
|
560 | If you want a watcher that is only ever called when *no* other |
|
|
561 | events are outstanding you have to set the priority to "EV::MINPRI". |
491 | |
562 | |
492 | The process will not block as long as any idle watchers are active, |
563 | The process will not block as long as any idle watchers are active, |
493 | and they will be called repeatedly until stopped. |
564 | and they will be called repeatedly until stopped. |
|
|
565 | |
|
|
566 | For example, if you have idle watchers at priority 0 and 1, and an |
|
|
567 | I/O watcher at priority 0, then the idle watcher at priority 1 and |
|
|
568 | the I/O watcher will always run when ready. Only when the idle |
|
|
569 | watcher at priority 1 is stopped and the I/O watcher at priority 0 |
|
|
570 | is not pending with the 0-priority idle watcher be invoked. |
494 | |
571 | |
495 | The "idle_ns" variant doesn't start (activate) the newly created |
572 | The "idle_ns" variant doesn't start (activate) the newly created |
496 | watcher. |
573 | watcher. |
497 | |
574 | |
498 | PREPARE WATCHERS - customise your event loop! |
575 | PREPARE WATCHERS - customise your event loop! |
… | |
… | |
527 | or return; |
604 | or return; |
528 | |
605 | |
529 | # make the dispatcher handle any outstanding stuff |
606 | # make the dispatcher handle any outstanding stuff |
530 | ... not shown |
607 | ... not shown |
531 | |
608 | |
532 | # create an IO watcher for each and every socket |
609 | # create an I/O watcher for each and every socket |
533 | @snmp_watcher = ( |
610 | @snmp_watcher = ( |
534 | (map { EV::io $_, EV::READ, sub { } } |
611 | (map { EV::io $_, EV::READ, sub { } } |
535 | keys %{ $dispatcher->{_descriptors} }), |
612 | keys %{ $dispatcher->{_descriptors} }), |
536 | |
613 | |
537 | EV::timer +($event->[Net::SNMP::Dispatcher::_ACTIVE] |
614 | EV::timer +($event->[Net::SNMP::Dispatcher::_ACTIVE] |
… | |
… | |
570 | Call the callback before the event loop is resumed in the child |
647 | Call the callback before the event loop is resumed in the child |
571 | process after a fork. |
648 | process after a fork. |
572 | |
649 | |
573 | The "fork_ns" variant doesn't start (activate) the newly created |
650 | The "fork_ns" variant doesn't start (activate) the newly created |
574 | watcher. |
651 | watcher. |
|
|
652 | |
|
|
653 | PERL SIGNALS |
|
|
654 | While Perl signal handling (%SIG) is not affected by EV, the behaviour |
|
|
655 | with EV is as the same as any other C library: Perl-signals will only be |
|
|
656 | handled when Perl runs, which means your signal handler might be invoked |
|
|
657 | only the next time an event callback is invoked. |
|
|
658 | |
|
|
659 | The solution is to use EV signal watchers (see "EV::signal"), which will |
|
|
660 | ensure proper operations with regards to other event watchers. |
|
|
661 | |
|
|
662 | If you cannot do this for whatever reason, you can also force a watcher |
|
|
663 | to be called on every event loop iteration by installing a "EV::check" |
|
|
664 | watcher: |
|
|
665 | |
|
|
666 | my $async_check = EV::check sub { }; |
|
|
667 | |
|
|
668 | This ensures that perl shortly gets into control for a short time, and |
|
|
669 | also ensures slower overall operation. |
575 | |
670 | |
576 | THREADS |
671 | THREADS |
577 | Threads are not supported by this module in any way. Perl pseudo-threads |
672 | Threads are not supported by this module in any way. Perl pseudo-threads |
578 | is evil stuff and must die. As soon as Perl gains real threads I will |
673 | is evil stuff and must die. As soon as Perl gains real threads I will |
579 | work on thread support for it. |
674 | work on thread support for it. |
… | |
… | |
594 | |
689 | |
595 | On win32, there is no notion of fork so all this doesn't apply, of |
690 | On win32, there is no notion of fork so all this doesn't apply, of |
596 | course. |
691 | course. |
597 | |
692 | |
598 | SEE ALSO |
693 | SEE ALSO |
599 | EV::DNS. |
694 | EV::ADNS (asynchronous dns), Glib::EV (makes Glib/Gtk2 use EV as event |
|
|
695 | loop), Coro::EV (efficient coroutines with EV). |
600 | |
696 | |
601 | AUTHOR |
697 | AUTHOR |
602 | Marc Lehmann <schmorp@schmorp.de> |
698 | Marc Lehmann <schmorp@schmorp.de> |
603 | http://home.schmorp.de/ |
699 | http://home.schmorp.de/ |
604 | |
700 | |