ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.pm
(Generate patch)

Comparing EV/EV.pm (file contents):
Revision 1.1 by root, Fri Oct 26 16:50:05 2007 UTC vs.
Revision 1.7 by root, Sat Oct 27 14:54:20 2007 UTC

4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use EV; 7 use EV;
8 8
9 # TIMER
10
11 my $w = EV::timer 2, 0, sub {
12 warn "is called after 2s";
13 };
14
15 my $w = EV::timer 2, 1, sub {
16 warn "is called roughly every 2s (repeat = 1)";
17 };
18
19 undef $w; # destroy event watcher again
20
21 # IO
22
23 my $w = EV::timer_abs 0, 60, sub {
24 warn "is called every minute, on the minute, exactly";
25 };
26
27 my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub {
28 my ($w, $events) = @_; # all callbacks get the watcher object and event mask
29 if ($events & EV::TIMEOUT) {
30 warn "nothign received on stdin for 10 seconds, retrying";
31 } else {
32 warn "stdin is readable, you entered: ", <STDIN>;
33 }
34 };
35 $w->timeout (10);
36
37 # MAINLOOP
38 EV::dispatch; # loop as long as watchers are active
39 EV::loop; # the same thing
40 EV::loop EV::LOOP_ONCE;
41 EV::loop EV::LOOP_ONSHOT;
42
9=head1 DESCRIPTION 43=head1 DESCRIPTION
10 44
11This module provides an interface to libevent 45This module provides an interface to libevent
12(L<http://monkey.org/~provos/libevent/>). 46(L<http://monkey.org/~provos/libevent/>). You probably should acquaint
47yourself with its documentation and source code to be able to use this
48module fully.
49
50Please note thta this module disables the libevent EPOLL method by
51default, see BUGS, below, if you need to enable it.
13 52
14=cut 53=cut
15 54
16package EV; 55package EV;
17 56
25 64
26=head1 FUNCTIONAL INTERFACE 65=head1 FUNCTIONAL INTERFACE
27 66
28=over 4 67=over 4
29 68
69=item $EV::NPRI
70
71How many priority levels are available.
72
73=item $time = EV::now
74
75Returns the time in (fractional) seconds since the epoch.
76
77=item $version = EV::version
78
79=item $method = EV::method
80
81Return version string and event polling method used.
82
83=item EV::loop $flags # EV::LOOP_ONCE, EV::LOOP_ONESHOT
84
85=item EV::loopexit $after
86
87Exit any active loop or dispatch after C<$after> seconds or immediately if
88C<$after> is missing or zero.
89
90=item EV::dispatch
91
92Same as C<EV::loop 0>.
93
94=item EV::event $callback
95
96Creates a new event watcher waiting for nothing, calling the given callback.
97
98=item my $w = EV::io $fileno_or_fh, $eventmask, $callback
99
100=item my $w = EV::io_ns $fileno_or_fh, $eventmask, $callback
101
102As long as the returned watcher object is alive, call the C<$callback>
103when the events specified in C<$eventmask> happen. Initially, the timeout
104is disabled.
105
106Youc an additionall set a timeout to occur on the watcher, but note that
107this timeout will not be reset when you get an I/O event in the EV::PERSIST
108case, and reaching a timeout will always stop the watcher even in the
109EV::PERSIST case.
110
111If you want a timeout to occur only after a specific time of inactivity, set
112a repeating timeout and do NOT use EV::PERSIST.
113
114Eventmask can be one or more of these constants ORed together:
115
116 EV::READ wait until read() wouldn't block anymore
117 EV::WRITE wait until write() wouldn't block anymore
118 EV::PERSIST stay active after a (non-timeout) event occured
119
120The C<io_ns> variant doesn't add/start the newly created watcher.
121
122=item my $w = EV::timer $after, $repeat, $callback
123
124=item my $w = EV::timer_ns $after, $repeat, $callback
125
126Calls the callback after C<$after> seconds. If C<$repeat> is true, the
127timer will be restarted after the callback returns. This means that the
128callback would be called roughly every C<$after> seconds, prolonged by the
129time the callback takes.
130
131The C<timer_ns> variant doesn't add/start the newly created watcher.
132
133=item my $w = EV::timer_abs $at, $interval, $callback
134
135=item my $w = EV::timer_abs_ns $at, $interval, $callback
136
137Similar to EV::timer, but the time is given as an absolute point in time
138(C<$at>), plus an optional C<$interval>.
139
140If the C<$interval> is zero, then the callback will be called at the time
141C<$at> if that is in the future, or as soon as possible if its in the
142past. It will not automatically repeat.
143
144If the C<$interval> is nonzero, then the watcher will always be scheduled
145to time out at the next C<$at + integer * $interval> time.
146
147This can be used to schedule a callback to run at very regular intervals,
148as long as the processing time is less then the interval (otherwise
149obviously events will be skipped).
150
151Another way to think about it (for the mathematically inclined) is that
152C<timer_abs> will try to tun the callback at the next possible time where
153C<$time = $at (mod $interval)>, regardless of any time jumps.
154
155The C<timer_abs_ns> variant doesn't add/start the newly created watcher.
156
157=item my $w = EV::signal $signum, $callback
158
159=item my $w = EV::signal_ns $signum, $callback
160
161Call the callback when signal $signum is received.
162
163The C<signal_ns> variant doesn't add/start the newly created watcher.
164
30=back 165=back
31 166
167=head1 THE EV::Event CLASS
32 168
33=head1 OBJECT-ORIENTED INTERFACE 169All EV functions creating an event watcher (designated by C<my $w =>
34 170above) support the following methods on the returned watcher object:
35The object oriented interface lets you configure your own encoding or
36decoding style, within the limits of supported formats.
37 171
38=over 4 172=over 4
39 173
174=item $w->add ($timeout)
175
176Stops and (re-)starts the event watcher, setting the optional timeout to
177the given value, or clearing the timeout if none is given.
178
179=item $w->start
180
181Stops and (re-)starts the event watcher without touching the timeout.
182
183=item $w->del
184
185=item $w->stop
186
187Stop the event watcher if it was started.
188
189=item $current_callback = $w->cb
190
191=item $old_callback = $w->cb ($new_callback)
192
193Return the previously set callback and optionally set a new one.
194
195=item $current_fh = $w->fh
196
197=item $old_fh = $w->fh ($new_fh)
198
199Returns the previously set filehandle and optionally set a new one.
200
201=item $current_eventmask = $w->events
202
203=item $old_eventmask = $w->events ($new_eventmask)
204
205Returns the previously set event mask and optionally set a new one.
206
207=item $w->timeout ($after, $repeat)
208
209Resets the timeout (see C<EV::timer> for details).
210
211=item $w->timeout_abs ($at, $interval)
212
213Resets the timeout (see C<EV::timer_abs> for details).
214
215=item $w->priority_set ($priority)
216
217Set the priority of the watcher to C<$priority> (0 <= $priority < $EV::NPRI).
218
40=back 219=back
41 220
42=head1 BUGS 221=head1 BUGS
222
223Lots. Libevent itself isn't well tested and rather buggy, and this module
224is quite new at the moment.
225
226Please note that the epoll method is not, in general, reliable in programs
227that use fork (even if no libveent calls are being made in the forked
228process). If your program behaves erratically, try setting the environment
229variable C<EVENT_NOEPOLL> first when running the program.
230
231In general, if you fork, then you can only use the EV module in one of the
232children.
43 233
44=cut 234=cut
45 235
46our $NPRI = 4; 236our $NPRI = 4;
47our $BASE = init; 237our $BASE = init;
48priority_init $NPRI; 238priority_init $NPRI;
49 239
240push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"];
241
501; 2421;
243
244=head1 SEE ALSO
245
246 L<EV::DNS>, L<event(3)>, L<event.h>, L<evdns.h>.
247 L<EV::AnyEvent>.
51 248
52=head1 AUTHOR 249=head1 AUTHOR
53 250
54 Marc Lehmann <schmorp@schmorp.de> 251 Marc Lehmann <schmorp@schmorp.de>
55 http://home.schmorp.de/ 252 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines