… | |
… | |
2 | |
2 | |
3 | EV - perl interface to libevent, monkey.org/~provos/libevent/ |
3 | EV - perl interface to libevent, monkey.org/~provos/libevent/ |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use EV; |
7 | use EV; |
8 | |
8 | |
9 | # TIMER |
9 | # TIMER |
10 | |
10 | |
11 | my $w = EV::timer 2, 0, sub { |
11 | my $w = EV::timer 2, 0, sub { |
12 | warn "is called after 2s"; |
12 | warn "is called after 2s"; |
13 | }; |
13 | }; |
14 | |
14 | |
15 | my $w = EV::timer 2, 1, sub { |
15 | my $w = EV::timer 2, 1, sub { |
16 | warn "is called roughly every 2s (repeat = 1)"; |
16 | warn "is called roughly every 2s (repeat = 1)"; |
17 | }; |
17 | }; |
18 | |
18 | |
19 | undef $w; # destroy event watcher again |
19 | undef $w; # destroy event watcher again |
20 | |
20 | |
21 | # IO |
|
|
22 | |
|
|
23 | my $w = EV::timer_abs 0, 60, sub { |
21 | my $w = EV::timer_abs 0, 60, sub { |
24 | warn "is called every minute, on the minute, exactly"; |
22 | warn "is called every minute, on the minute, exactly"; |
25 | }; |
23 | }; |
26 | |
24 | |
|
|
25 | # IO |
|
|
26 | |
27 | my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub { |
27 | my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub { |
28 | my ($w, $events) = @_; # all callbacks get the watcher object and event mask |
28 | my ($w, $revents) = @_; # all callbacks get the watcher object and event mask |
29 | if ($events & EV::TIMEOUT) { |
29 | if ($revents & EV::TIMEOUT) { |
30 | warn "nothing received on stdin for 10 seconds, retrying"; |
30 | warn "nothing received on stdin for 10 seconds, retrying"; |
31 | } else { |
31 | } else { |
32 | warn "stdin is readable, you entered: ", <STDIN>; |
32 | warn "stdin is readable, you entered: ", <STDIN>; |
33 | } |
33 | } |
34 | }; |
34 | }; |
35 | $w->timeout (10); |
35 | $w->timeout (10); |
|
|
36 | |
|
|
37 | my $w = EV::timed_io \*STDIN, EV::READ, 30, sub { |
|
|
38 | my ($w, $revents) = @_; |
|
|
39 | if ($revents & EV::TIMEOUT) { |
|
|
40 | warn "nothing entered within 30 seconds, bye bye.\n"; |
|
|
41 | $w->stop; |
|
|
42 | } else { |
|
|
43 | my $line = <STDIN>; |
|
|
44 | warn "you entered something, you again have 30 seconds.\n"; |
|
|
45 | } |
|
|
46 | }; |
|
|
47 | |
|
|
48 | # SIGNALS |
|
|
49 | |
|
|
50 | my $w = EV::signal 'QUIT', sub { |
|
|
51 | warn "sigquit received\n"; |
|
|
52 | }; |
|
|
53 | |
|
|
54 | my $w = EV::signal 3, sub { |
|
|
55 | warn "sigquit received (this is GNU/Linux, right?)\n"; |
|
|
56 | }; |
36 | |
57 | |
|
|
58 | # CHILD/PID STATUS CHANGES |
|
|
59 | |
|
|
60 | my $w = EV::child 666, sub { |
|
|
61 | my ($w, $revents, $status) = @_; |
|
|
62 | }; |
|
|
63 | |
37 | # MAINLOOP |
64 | # MAINLOOP |
38 | EV::dispatch; # loop as long as watchers are active |
65 | EV::dispatch; # loop as long as watchers are active |
39 | EV::loop; # the same thing |
66 | EV::loop; # the same thing |
40 | EV::loop EV::LOOP_ONCE; |
67 | EV::loop EV::LOOP_ONESHOT; # block until some events could be handles |
41 | EV::loop EV::LOOP_ONSHOT; |
68 | EV::loop EV::LOOP_NONBLOCK; # check and handle some events, but do not wait |
42 | |
69 | |
43 | =head1 DESCRIPTION |
70 | =head1 DESCRIPTION |
44 | |
71 | |
45 | This module provides an interface to libevent |
72 | This module provides an interface to libev |
46 | (L<http://monkey.org/~provos/libevent/>). You probably should acquaint |
73 | (L<http://software.schmorp.de/pkg/libev.html>). You probably should |
47 | yourself with its documentation and source code to be able to use this |
74 | acquaint yourself with its documentation and source code to be able to use |
48 | module fully. |
75 | this module fully. |
49 | |
|
|
50 | Please note thta this module disables the libevent EPOLL method by |
|
|
51 | default, see BUGS, below, if you need to enable it. |
|
|
52 | |
76 | |
53 | =cut |
77 | =cut |
54 | |
78 | |
55 | package EV; |
79 | package EV; |
56 | |
80 | |
57 | use strict; |
81 | use strict; |
58 | |
82 | |
59 | BEGIN { |
83 | BEGIN { |
60 | our $VERSION = '0.02'; |
84 | our $VERSION = '0.1'; |
61 | use XSLoader; |
85 | use XSLoader; |
62 | XSLoader::load "EV", $VERSION; |
86 | XSLoader::load "EV", $VERSION; |
63 | } |
87 | } |
|
|
88 | |
|
|
89 | @EV::Io::ISA = |
|
|
90 | @EV::Timer::ISA = |
|
|
91 | @EV::Periodic::ISA = |
|
|
92 | @EV::Signal::ISA = |
|
|
93 | @EV::Idle::ISA = |
|
|
94 | @EV::Prepare::ISA = |
|
|
95 | @EV::Check::ISA = |
|
|
96 | @EV::Child::ISA = "EV::Watcher"; |
64 | |
97 | |
65 | =head1 BASIC INTERFACE |
98 | =head1 BASIC INTERFACE |
66 | |
99 | |
67 | =over 4 |
100 | =over 4 |
68 | |
101 | |
… | |
… | |
178 | C<timer_abs> will try to tun the callback at the next possible time where |
211 | C<timer_abs> will try to tun the callback at the next possible time where |
179 | C<$time = $at (mod $interval)>, regardless of any time jumps. |
212 | C<$time = $at (mod $interval)>, regardless of any time jumps. |
180 | |
213 | |
181 | The C<timer_abs_ns> variant doesn't add/start the newly created watcher. |
214 | The C<timer_abs_ns> variant doesn't add/start the newly created watcher. |
182 | |
215 | |
183 | =item my $w = EV::signal $signum, $callback |
216 | =item my $w = EV::signal $signal, $callback |
184 | |
217 | |
185 | =item my $w = EV::signal_ns $signum, $callback |
218 | =item my $w = EV::signal_ns $signal, $callback |
186 | |
219 | |
187 | Call the callback when signal $signum is received. |
220 | Call the callback when $signal is received (the signal can be specified |
|
|
221 | by number or by name, just as with kill or %SIG). Signal watchers are |
|
|
222 | persistent no natter what. |
|
|
223 | |
|
|
224 | EV will grab the signal for the process (the kernel only allows one |
|
|
225 | component to receive signals) when you start a signal watcher, and |
|
|
226 | removes it again when you stop it. Pelr does the same when you add/remove |
|
|
227 | callbacks to %SIG, so watch out. |
|
|
228 | |
|
|
229 | Unfortunately, only one handler can be registered per signal. Screw |
|
|
230 | libevent. |
188 | |
231 | |
189 | The C<signal_ns> variant doesn't add/start the newly created watcher. |
232 | The C<signal_ns> variant doesn't add/start the newly created watcher. |
190 | |
233 | |
191 | =back |
234 | =back |
192 | |
235 | |
… | |
… | |
250 | |
293 | |
251 | Set the priority of the watcher to C<$priority> (0 <= $priority < $EV::NPRI). |
294 | Set the priority of the watcher to C<$priority> (0 <= $priority < $EV::NPRI). |
252 | |
295 | |
253 | =back |
296 | =back |
254 | |
297 | |
255 | =head1 BUGS |
298 | =head1 THREADS |
256 | |
299 | |
257 | Lots. Libevent itself isn't well tested and rather buggy, and this module |
300 | Threads are not supported by this in any way. Perl pseudo-threads is evil |
258 | is quite new at the moment. |
301 | and must die. |
259 | |
|
|
260 | Please note that the epoll method is not, in general, reliable in programs |
|
|
261 | that use fork (even if no libveent calls are being made in the forked |
|
|
262 | process). If your program behaves erratically, try setting the environment |
|
|
263 | variable C<EVENT_NOEPOLL> first when running the program. |
|
|
264 | |
|
|
265 | In general, if you fork, then you can only use the EV module in one of the |
|
|
266 | children. |
|
|
267 | |
302 | |
268 | =cut |
303 | =cut |
269 | |
304 | |
270 | our $DIED = sub { |
305 | our $DIED = sub { |
271 | warn "EV: error in callback (ignoring): $@"; |
306 | warn "EV: error in callback (ignoring): $@"; |
272 | }; |
307 | }; |
273 | |
308 | |
274 | our $NPRI = 4; |
309 | init; |
275 | our $BASE = init; |
|
|
276 | priority_init $NPRI; |
|
|
277 | |
310 | |
278 | push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"]; |
311 | push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"]; |
279 | |
312 | |
280 | 1; |
313 | 1; |
281 | |
314 | |