… | |
… | |
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 |
… | |
… | |
121 | |
122 | |
122 | EV::once doesn't return anything: the watchers stay active till |
123 | EV::once doesn't return anything: the watchers stay active till |
123 | 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 |
124 | the callback invoked. |
125 | the callback invoked. |
125 | |
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 | |
126 | WATCHER OBJECTS |
137 | WATCHER OBJECTS |
127 | 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 |
128 | 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, |
129 | you would create an EV::io watcher for that: |
140 | you would create an EV::io watcher for that: |
130 | |
141 | |
… | |
… | |
139 | received events. |
150 | received events. |
140 | |
151 | |
141 | 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 |
142 | 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 |
143 | 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, |
144 | 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 |
145 | 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 |
146 | (which uses EV::TIMEOUT). |
157 | (which uses EV::TIMEOUT). |
147 | |
158 | |
148 | 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 |
149 | 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 |
… | |
… | |
167 | stopped watchers). |
178 | stopped watchers). |
168 | |
179 | |
169 | $w->stop |
180 | $w->stop |
170 | 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 |
171 | (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 |
172 | callback invocation), regardless of wether the watcher was active or |
183 | callback invocation), regardless of whether the watcher was active |
173 | not. |
184 | or not. |
174 | |
185 | |
175 | $bool = $w->is_active |
186 | $bool = $w->is_active |
176 | Returns true if the watcher is active, false otherwise. |
187 | Returns true if the watcher is active, false otherwise. |
177 | |
188 | |
178 | $current_data = $w->data |
189 | $current_data = $w->data |
… | |
… | |
201 | The default priority of any newly-created watcher is 0. |
212 | The default priority of any newly-created watcher is 0. |
202 | |
213 | |
203 | 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 |
204 | are subject to almost certain change. |
215 | are subject to almost certain change. |
205 | |
216 | |
206 | $w->trigger ($revents) |
217 | $w->invoke ($revents) |
207 | 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. |
208 | |
229 | |
209 | $previous_state = $w->keepalive ($bool) |
230 | $previous_state = $w->keepalive ($bool) |
210 | Normally, "EV::loop" will return when there are no active watchers |
231 | Normally, "EV::loop" will return when there are no active watchers |
211 | (which is a "deadlock" because no progress can be made anymore). |
232 | (which is a "deadlock" because no progress can be made anymore). |
212 | 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 |
… | |
… | |
227 | returning. |
248 | returning. |
228 | |
249 | |
229 | The initial value for keepalive is true (enabled), and you cna |
250 | The initial value for keepalive is true (enabled), and you cna |
230 | change it any time. |
251 | change it any time. |
231 | |
252 | |
232 | 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 |
233 | the event loop from running just because of that watcher. |
254 | the event loop from running just because of that watcher. |
234 | |
255 | |
235 | my $udp_socket = ... |
256 | my $udp_socket = ... |
236 | my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... }; |
257 | my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... }; |
237 | $udp_watcher->keepalive (0); |
258 | $udp_watcher->keepalive (0); |
238 | |
259 | |
239 | WATCHER TYPES |
260 | WATCHER TYPES |
240 | Each of the following subsections describes a single watcher type. |
261 | Each of the following subsections describes a single watcher type. |
241 | |
262 | |
242 | IO WATCHERS - is this file descriptor readable or writable? |
263 | I/O WATCHERS - is this file descriptor readable or writable? |
243 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
264 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
244 | $w = EV::io_ns $fileno_or_fh, $eventmask, $callback |
265 | $w = EV::io_ns $fileno_or_fh, $eventmask, $callback |
245 | 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 |
246 | when at least one of events specified in $eventmask occurs. |
267 | when at least one of events specified in $eventmask occurs. |
247 | |
268 | |
… | |
… | |
524 | Can be used to query the actual interval used. |
545 | Can be used to query the actual interval used. |
525 | |
546 | |
526 | IDLE WATCHERS - when you've got nothing better to do... |
547 | IDLE WATCHERS - when you've got nothing better to do... |
527 | $w = EV::idle $callback |
548 | $w = EV::idle $callback |
528 | $w = EV::idle_ns $callback |
549 | $w = EV::idle_ns $callback |
529 | Call the callback when there are no pending io, timer/periodic, |
550 | Call the callback when there are no other pending watchers of the |
530 | signal or child events, i.e. when the process is idle. |
551 | same or higher priority (excluding check, prepare and other idle |
|
|
552 | watchers of the same or lower priority, of course). They are called |
|
|
553 | idle watchers because when the watcher is the highest priority |
|
|
554 | pending event in the process, the process is considered to be idle |
|
|
555 | at that priority. |
|
|
556 | |
|
|
557 | If you want a watcher that is only ever called when *no* other |
|
|
558 | events are outstanding you have to set the priority to "EV::MINPRI". |
531 | |
559 | |
532 | The process will not block as long as any idle watchers are active, |
560 | The process will not block as long as any idle watchers are active, |
533 | and they will be called repeatedly until stopped. |
561 | and they will be called repeatedly until stopped. |
|
|
562 | |
|
|
563 | For example, if you have idle watchers at priority 0 and 1, and an |
|
|
564 | I/O watcher at priority 0, then the idle watcher at priority 1 and |
|
|
565 | the I/O watcher will always run when ready. Only when the idle |
|
|
566 | watcher at priority 1 is stopped and the I/O watcher at priority 0 |
|
|
567 | is not pending with the 0-priority idle watcher be invoked. |
534 | |
568 | |
535 | The "idle_ns" variant doesn't start (activate) the newly created |
569 | The "idle_ns" variant doesn't start (activate) the newly created |
536 | watcher. |
570 | watcher. |
537 | |
571 | |
538 | PREPARE WATCHERS - customise your event loop! |
572 | PREPARE WATCHERS - customise your event loop! |
… | |
… | |
567 | or return; |
601 | or return; |
568 | |
602 | |
569 | # make the dispatcher handle any outstanding stuff |
603 | # make the dispatcher handle any outstanding stuff |
570 | ... not shown |
604 | ... not shown |
571 | |
605 | |
572 | # create an IO watcher for each and every socket |
606 | # create an I/O watcher for each and every socket |
573 | @snmp_watcher = ( |
607 | @snmp_watcher = ( |
574 | (map { EV::io $_, EV::READ, sub { } } |
608 | (map { EV::io $_, EV::READ, sub { } } |
575 | keys %{ $dispatcher->{_descriptors} }), |
609 | keys %{ $dispatcher->{_descriptors} }), |
576 | |
610 | |
577 | EV::timer +($event->[Net::SNMP::Dispatcher::_ACTIVE] |
611 | EV::timer +($event->[Net::SNMP::Dispatcher::_ACTIVE] |
… | |
… | |
610 | Call the callback before the event loop is resumed in the child |
644 | Call the callback before the event loop is resumed in the child |
611 | process after a fork. |
645 | process after a fork. |
612 | |
646 | |
613 | The "fork_ns" variant doesn't start (activate) the newly created |
647 | The "fork_ns" variant doesn't start (activate) the newly created |
614 | watcher. |
648 | watcher. |
|
|
649 | |
|
|
650 | PERL SIGNALS |
|
|
651 | While Perl signal handling (%SIG) is not affected by EV, the behaviour |
|
|
652 | with EV is as the same as any other C library: Perl-signals will only be |
|
|
653 | handled when Perl runs, which means your signal handler might be invoked |
|
|
654 | only the next time an event callback is invoked. |
|
|
655 | |
|
|
656 | The solution is to use EV signal watchers (see "EV::signal"), which will |
|
|
657 | ensure proper operations with regards to other event watchers. |
|
|
658 | |
|
|
659 | If you cannot do this for whatever reason, you can also force a watcher |
|
|
660 | to be called on every event loop iteration by installing a "EV::check" |
|
|
661 | watcher: |
|
|
662 | |
|
|
663 | my $async_check = EV::check sub { }; |
|
|
664 | |
|
|
665 | This ensures that perl shortly gets into control for a short time, and |
|
|
666 | also ensures slower overall operation. |
615 | |
667 | |
616 | THREADS |
668 | THREADS |
617 | Threads are not supported by this module in any way. Perl pseudo-threads |
669 | Threads are not supported by this module in any way. Perl pseudo-threads |
618 | is evil stuff and must die. As soon as Perl gains real threads I will |
670 | is evil stuff and must die. As soon as Perl gains real threads I will |
619 | work on thread support for it. |
671 | work on thread support for it. |