… | |
… | |
9 | |
9 | |
10 | my $w = EV::timer 2, 0, sub { |
10 | my $w = EV::timer 2, 0, sub { |
11 | warn "is called after 2s"; |
11 | warn "is called after 2s"; |
12 | }; |
12 | }; |
13 | |
13 | |
14 | my $w = EV::timer 2, 1, sub { |
14 | my $w = EV::timer 2, 2, sub { |
15 | warn "is called roughly every 2s (repeat = 1)"; |
15 | warn "is called roughly every 2s (repeat = 2)"; |
16 | }; |
16 | }; |
17 | |
17 | |
18 | undef $w; # destroy event watcher again |
18 | undef $w; # destroy event watcher again |
19 | |
19 | |
20 | my $w = EV::periodic 0, 60, sub { |
20 | my $w = EV::periodic 0, 60, 0, sub { |
21 | warn "is called every minute, on the minute, exactly"; |
21 | warn "is called every minute, on the minute, exactly"; |
22 | }; |
22 | }; |
23 | |
23 | |
24 | # IO |
24 | # IO |
25 | |
25 | |
26 | my $w = EV::io *STDIN, EV::READ, sub { |
26 | my $w = EV::io *STDIN, EV::READ, sub { |
27 | my ($w, $revents) = @_; # all callbacks get the watcher object and event mask |
27 | my ($w, $revents) = @_; # all callbacks receive the watcher and event mask |
28 | warn "stdin is readable, you entered: ", <STDIN>; |
28 | warn "stdin is readable, you entered: ", <STDIN>; |
29 | }; |
29 | }; |
30 | |
30 | |
31 | # SIGNALS |
31 | # SIGNALS |
32 | |
32 | |
33 | my $w = EV::signal 'QUIT', sub { |
33 | my $w = EV::signal 'QUIT', sub { |
34 | warn "sigquit received\n"; |
34 | warn "sigquit received\n"; |
35 | }; |
35 | }; |
36 | |
36 | |
37 | my $w = EV::signal 3, sub { |
|
|
38 | warn "sigquit received (this is GNU/Linux, right?)\n"; |
|
|
39 | }; |
|
|
40 | |
|
|
41 | # CHILD/PID STATUS CHANGES |
37 | # CHILD/PID STATUS CHANGES |
42 | |
38 | |
43 | my $w = EV::child 666, sub { |
39 | my $w = EV::child 666, sub { |
44 | my ($w, $revents, $status) = @_; |
40 | my ($w, $revents) = @_; |
|
|
41 | my $status = $w->rstatus; |
45 | }; |
42 | }; |
46 | |
43 | |
47 | # MAINLOOP |
44 | # MAINLOOP |
48 | EV::loop; # loop until EV::loop_done is called |
45 | EV::loop; # loop until EV::unloop is called or all watchers stop |
49 | EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled |
46 | EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled |
50 | EV::loop EV::LOOP_NONBLOCK; # try to handle same events, but do not block |
47 | EV::loop EV::LOOP_NONBLOCK; # try to handle same events, but do not block |
51 | |
48 | |
52 | DESCRIPTION |
49 | DESCRIPTION |
53 | This module provides an interface to libev |
50 | This module provides an interface to libev |
… | |
… | |
67 | $time = EV::now |
64 | $time = EV::now |
68 | Returns the time the last event loop iteration has been started. |
65 | Returns the time the last event loop iteration has been started. |
69 | This is the time that (relative) timers are based on, and refering |
66 | This is the time that (relative) timers are based on, and refering |
70 | to it is usually faster then calling EV::time. |
67 | to it is usually faster then calling EV::time. |
71 | |
68 | |
72 | $method = EV::ev_method |
69 | $method = EV::method |
73 | Returns an integer describing the backend used by libev |
70 | Returns an integer describing the backend used by libev |
74 | (EV::METHOD_SELECT or EV::METHOD_EPOLL). |
71 | (EV::METHOD_SELECT or EV::METHOD_EPOLL). |
75 | |
72 | |
76 | EV::loop [$flags] |
73 | EV::loop [$flags] |
77 | Begin checking for events and calling callbacks. It returns when a |
74 | Begin checking for events and calling callbacks. It returns when a |
78 | callback calls EV::loop_done. |
75 | callback calls EV::unloop. |
79 | |
76 | |
80 | The $flags argument can be one of the following: |
77 | The $flags argument can be one of the following: |
81 | |
78 | |
82 | 0 as above |
79 | 0 as above |
83 | EV::LOOP_ONESHOT block at most once (wait, but do not loop) |
80 | EV::LOOP_ONESHOT block at most once (wait, but do not loop) |
84 | EV::LOOP_NONBLOCK do not block at all (fetch/handle events but do not wait) |
81 | EV::LOOP_NONBLOCK do not block at all (fetch/handle events but do not wait) |
85 | |
82 | |
86 | EV::loop_done [$how] |
83 | EV::unloop [$how] |
87 | When called with no arguments or an argument of 1, makes the |
84 | When called with no arguments or an argument of EV::UNLOOP_ONE, |
88 | innermost call to EV::loop return. |
85 | makes the innermost call to EV::loop return. |
89 | |
86 | |
90 | When called with an agrument of 2, all calls to EV::loop will return |
87 | When called with an argument of EV::UNLOOP_ALL, all calls to |
91 | as fast as possible. |
88 | EV::loop will return as fast as possible. |
92 | |
89 | |
93 | WATCHER |
90 | WATCHER |
94 | A watcher is an object that gets created to record your interest in some |
91 | A watcher is an object that gets created to record your interest in some |
95 | event. For instance, if you want to wait for STDIN to become readable, |
92 | event. For instance, if you want to wait for STDIN to become readable, |
96 | you would create an EV::io watcher for that: |
93 | you would create an EV::io watcher for that: |
… | |
… | |
115 | In the rare case where one wants to create a watcher but not start it at |
112 | In the rare case where one wants to create a watcher but not start it at |
116 | the same time, each constructor has a variant with a trailing "_ns" in |
113 | the same time, each constructor has a variant with a trailing "_ns" in |
117 | its name, e.g. EV::io has a non-starting variant EV::io_ns and so on. |
114 | its name, e.g. EV::io has a non-starting variant EV::io_ns and so on. |
118 | |
115 | |
119 | Please note that a watcher will automatically be stopped when the |
116 | Please note that a watcher will automatically be stopped when the |
120 | watcher object is returned, so you *need* to keep the watcher objects |
117 | watcher object is destroyed, so you *need* to keep the watcher objects |
121 | returned by the constructors. |
118 | returned by the constructors. |
|
|
119 | |
|
|
120 | Also, all methods changing some aspect of a watcher (->set, ->priority, |
|
|
121 | ->fh and so on) automatically stop and start it again if it is active, |
|
|
122 | which means pending events get lost. |
122 | |
123 | |
123 | WATCHER TYPES |
124 | WATCHER TYPES |
124 | Now lets move to the existing watcher types and asociated methods. |
125 | Now lets move to the existing watcher types and asociated methods. |
125 | |
126 | |
126 | The following methods are available for all watchers. Then followes a |
127 | The following methods are available for all watchers. Then followes a |
… | |
… | |
141 | not. |
142 | not. |
142 | |
143 | |
143 | $bool = $w->is_active |
144 | $bool = $w->is_active |
144 | Returns true if the watcher is active, false otherwise. |
145 | Returns true if the watcher is active, false otherwise. |
145 | |
146 | |
|
|
147 | $current_data = $w->data |
|
|
148 | $old_data = $w->data ($new_data) |
|
|
149 | Queries a freely usable data scalar on the watcher and optionally |
|
|
150 | changes it. This is a way to associate custom data with a watcher: |
|
|
151 | |
|
|
152 | my $w = EV::timer 60, 0, sub { |
|
|
153 | warn $_[0]->data; |
|
|
154 | }; |
|
|
155 | $w->data ("print me!"); |
|
|
156 | |
146 | $current_cb = $w->cb |
157 | $current_cb = $w->cb |
147 | $old_cb = $w->cb ($new_cb) |
158 | $old_cb = $w->cb ($new_cb) |
148 | Queries the callback on the watcher and optionally changes it. You |
159 | Queries the callback on the watcher and optionally changes it. You |
149 | cna do this at any time. |
160 | can do this at any time without the watcher restarting. |
|
|
161 | |
|
|
162 | $current_priority = $w->priority |
|
|
163 | $old_priority = $w->priority ($new_priority) |
|
|
164 | Queries the priority on the watcher and optionally changes it. |
|
|
165 | Pending watchers with higher priority will be invoked first. The |
|
|
166 | valid range of priorities lies between EV::MAXPRI (default 2) and |
|
|
167 | EV::MINPRI (default -2). If the priority is outside this range it |
|
|
168 | will automatically be normalised to the nearest valid priority. |
|
|
169 | |
|
|
170 | The default priority of any newly-created weatcher is 0. |
150 | |
171 | |
151 | $w->trigger ($revents) |
172 | $w->trigger ($revents) |
152 | Call the callback *now* with the given event mask. |
173 | Call the callback *now* with the given event mask. |
153 | |
174 | |
154 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
175 | $w = EV::io $fileno_or_fh, $eventmask, $callback |
… | |
… | |
181 | Calls the callback after $after seconds. If $repeat is non-zero, the |
202 | Calls the callback after $after seconds. If $repeat is non-zero, the |
182 | timer will be restarted (with the $repeat value as $after) after the |
203 | timer will be restarted (with the $repeat value as $after) after the |
183 | callback returns. |
204 | callback returns. |
184 | |
205 | |
185 | This means that the callback would be called roughly after $after |
206 | This means that the callback would be called roughly after $after |
186 | seconds, and then every $repeat seconds. "Roughly" because the time |
207 | seconds, and then every $repeat seconds. The timer does his best not |
187 | of callback processing is not taken into account, so the timer will |
208 | to drift, but it will not invoke the timer more often then once per |
188 | slowly drift. If that isn't acceptable, look at EV::periodic. |
209 | event loop iteration, and might drift in other cases. If that isn't |
|
|
210 | acceptable, look at EV::periodic, which can provide long-term stable |
|
|
211 | timers. |
189 | |
212 | |
190 | The timer is based on a monotonic clock, that is if somebody is |
213 | The timer is based on a monotonic clock, that is, if somebody is |
191 | sitting in front of the machine while the timer is running and |
214 | sitting in front of the machine while the timer is running and |
192 | changes the system clock, the timer will nevertheless run (roughly) |
215 | changes the system clock, the timer will nevertheless run (roughly) |
193 | the same time. |
216 | the same time. |
194 | |
217 | |
195 | The "timer_ns" variant doesn't start (activate) the newly created |
218 | The "timer_ns" variant doesn't start (activate) the newly created |
… | |
… | |
201 | |
224 | |
202 | $w->again |
225 | $w->again |
203 | Similar to the "start" method, but has special semantics for |
226 | Similar to the "start" method, but has special semantics for |
204 | repeating timers: |
227 | repeating timers: |
205 | |
228 | |
|
|
229 | If the timer is active and non-repeating, it will be stopped. |
|
|
230 | |
206 | If the timer is active and repeating, reset the timeout to occur |
231 | If the timer is active and repeating, reset the timeout to occur |
207 | $repeat seconds after now. |
232 | $repeat seconds after now. |
208 | |
233 | |
209 | If the timer is active and non-repeating, it will be stopped. |
|
|
210 | |
|
|
211 | If the timer is in active and repeating, start it. |
234 | If the timer is inactive and repeating, start it using the repeat |
|
|
235 | value. |
212 | |
236 | |
213 | Otherwise do nothing. |
237 | Otherwise do nothing. |
214 | |
238 | |
215 | This behaviour is useful when you have a timeout for some IO |
239 | This behaviour is useful when you have a timeout for some IO |
216 | operation. You create a timer object with the same value for $after |
240 | operation. You create a timer object with the same value for $after |
217 | and $repeat, and then, in the read/write watcher, run the "again" |
241 | and $repeat, and then, in the read/write watcher, run the "again" |
218 | method on the timeout. |
242 | method on the timeout. |
219 | |
243 | |
220 | $w = EV::periodic $at, $interval, $callback |
244 | $w = EV::periodic $at, $interval, $reschedule_cb, $callback |
221 | $w = EV::periodic_ns $at, $interval, $callback |
245 | $w = EV::periodic_ns $at, $interval, $reschedule_cb, $callback |
222 | Similar to EV::timer, but the time is given as an absolute point in |
246 | Similar to EV::timer, but is not based on relative timeouts but on |
223 | time ($at), plus an optional $interval. |
247 | absolute times. Apart from creating "simple" timers that trigger |
|
|
248 | "at" the specified time, it can also be used for non-drifting |
|
|
249 | absolute timers and more complex, cron-like, setups that are not |
|
|
250 | adversely affected by time jumps (i.e. when the system clock is |
|
|
251 | changed by explicit date -s or other means such as ntpd). It is also |
|
|
252 | the most complex watcher type in EV. |
224 | |
253 | |
225 | If the $interval is zero, then the callback will be called at the |
254 | It has three distinct "modes": |
226 | time $at if that is in the future, or as soon as possible if it is |
|
|
227 | in the past. It will not automatically repeat. |
|
|
228 | |
255 | |
229 | If the $interval is nonzero, then the watcher will always be |
256 | * absolute timer ($interval = $reschedule_cb = 0) |
230 | scheduled to time out at the next "$at + N * $interval" time. |
257 | This time simply fires at the wallclock time $at and doesn't |
|
|
258 | repeat. It will not adjust when a time jump occurs, that is, if |
|
|
259 | it is to be run at January 1st 2011 then it will run when the |
|
|
260 | system time reaches or surpasses this time. |
231 | |
261 | |
232 | This can be used to schedule a callback to run at very regular |
262 | * non-repeating interval timer ($interval > 0, $reschedule_cb = 0) |
233 | intervals, as long as the processing time is less then the interval |
263 | In this mode the watcher will always be scheduled to time out at |
234 | (otherwise obviously events will be skipped). |
264 | the next "$at + N * $interval" time (for some integer N) and |
|
|
265 | then repeat, regardless of any time jumps. |
235 | |
266 | |
|
|
267 | This can be used to create timers that do not drift with respect |
|
|
268 | to system time: |
|
|
269 | |
|
|
270 | my $hourly = EV::periodic 0, 3600, 0, sub { print "once/hour\n" }; |
|
|
271 | |
|
|
272 | That doesn't mean there will always be 3600 seconds in between |
|
|
273 | triggers, but only that the the clalback will be called when the |
|
|
274 | system time shows a full hour (UTC). |
|
|
275 | |
236 | Another way to think about it (for the mathematically inclined) is |
276 | Another way to think about it (for the mathematically inclined) |
237 | that EV::periodic will try to run the callback at the next possible |
277 | is that EV::periodic will try to run the callback in this mode |
238 | time where "$time = $at (mod $interval)", regardless of any time |
278 | at the next possible time where "$time = $at (mod $interval)", |
239 | jumps. |
279 | regardless of any time jumps. |
240 | |
280 | |
241 | This periodic timer is based on "wallclock time", that is, if the |
281 | * manual reschedule mode ($reschedule_cb = coderef) |
242 | clock changes ("ntp", "date -s" etc.), then the timer will |
282 | In this mode $interval and $at are both being ignored. Instead, |
243 | nevertheless run at the specified time. This means it will never |
283 | each time the periodic watcher gets scheduled, the reschedule |
244 | drift (it might jitter, but it will not drift). |
284 | callback ($reschedule_cb) will be called with the watcher as |
|
|
285 | first, and the current time as second argument. |
|
|
286 | |
|
|
287 | *This callback MUST NOT stop or destroy this or any other |
|
|
288 | periodic watcher, ever*. If you need to stop it, return 1e30 and |
|
|
289 | stop it afterwards. |
|
|
290 | |
|
|
291 | It must return the next time to trigger, based on the passed |
|
|
292 | time value (that is, the lowest time value larger than to the |
|
|
293 | second argument). It will usually be called just before the |
|
|
294 | callback will be triggered, but might be called at other times, |
|
|
295 | too. |
|
|
296 | |
|
|
297 | This can be used to create very complex timers, such as a timer |
|
|
298 | that triggers on each midnight, local time (actually 24 hours |
|
|
299 | after the last midnight, to keep the example simple. If you know |
|
|
300 | a way to do it correctly in about the same space (without |
|
|
301 | requiring elaborate modules), drop me a note :): |
|
|
302 | |
|
|
303 | my $daily = EV::periodic 0, 0, sub { |
|
|
304 | my ($w, $now) = @_; |
|
|
305 | |
|
|
306 | use Time::Local (); |
|
|
307 | my (undef, undef, undef, $d, $m, $y) = localtime $now; |
|
|
308 | 86400 + Time::Local::timelocal 0, 0, 0, $d, $m, $y |
|
|
309 | }, sub { |
|
|
310 | print "it's midnight or likely shortly after, now\n"; |
|
|
311 | }; |
245 | |
312 | |
246 | The "periodic_ns" variant doesn't start (activate) the newly created |
313 | The "periodic_ns" variant doesn't start (activate) the newly created |
247 | watcher. |
314 | watcher. |
248 | |
315 | |
249 | $w->set ($at, $interval) |
316 | $w->set ($at, $interval, $reschedule_cb) |
250 | Reconfigures the watcher, see the constructor above for details. Can |
317 | Reconfigures the watcher, see the constructor above for details. Can |
251 | be at any time. |
318 | be at any time. |
|
|
319 | |
|
|
320 | $w->again |
|
|
321 | Simply stops and starts the watcher again. |
252 | |
322 | |
253 | $w = EV::signal $signal, $callback |
323 | $w = EV::signal $signal, $callback |
254 | $w = EV::signal_ns $signal, $callback |
324 | $w = EV::signal_ns $signal, $callback |
255 | Call the callback when $signal is received (the signal can be |
325 | Call the callback when $signal is received (the signal can be |
256 | specified by number or by name, just as with kill or %SIG). |
326 | specified by number or by name, just as with kill or %SIG). |
… | |
… | |
266 | watcher. |
336 | watcher. |
267 | |
337 | |
268 | $w->set ($signal) |
338 | $w->set ($signal) |
269 | Reconfigures the watcher, see the constructor above for details. Can |
339 | Reconfigures the watcher, see the constructor above for details. Can |
270 | be at any time. |
340 | be at any time. |
|
|
341 | |
|
|
342 | $current_signum = $w->signal |
|
|
343 | $old_signum = $w->signal ($new_signal) |
|
|
344 | Returns the previously set signal (always as a number not name) and |
|
|
345 | optionally set a new one. |
271 | |
346 | |
272 | $w = EV::child $pid, $callback |
347 | $w = EV::child $pid, $callback |
273 | $w = EV::child_ns $pid, $callback |
348 | $w = EV::child_ns $pid, $callback |
274 | Call the callback when a status change for pid $pid (or any pid if |
349 | Call the callback when a status change for pid $pid (or any pid if |
275 | $pid is 0) has been received. More precisely: when the process |
350 | $pid is 0) has been received. More precisely: when the process |
276 | receives a SIGCHLD, EV will fetch the outstanding exit/wait status |
351 | receives a SIGCHLD, EV will fetch the outstanding exit/wait status |
277 | for all changed/zombie children and call the callback. |
352 | for all changed/zombie children and call the callback. |
278 | |
353 | |
279 | Unlike all other callbacks, this callback will be called with an |
354 | You can access both status and pid by using the "rstatus" and "rpid" |
280 | additional third argument which is the exit status. See the |
355 | methods on the watcher object. |
281 | "waitpid" function for details. |
|
|
282 | |
356 | |
283 | You can have as many pid watchers per pid as you want. |
357 | You can have as many pid watchers per pid as you want. |
284 | |
358 | |
285 | The "child_ns" variant doesn't start (activate) the newly created |
359 | The "child_ns" variant doesn't start (activate) the newly created |
286 | watcher. |
360 | watcher. |
287 | |
361 | |
288 | $w->set ($pid) |
362 | $w->set ($pid) |
289 | Reconfigures the watcher, see the constructor above for details. Can |
363 | Reconfigures the watcher, see the constructor above for details. Can |
290 | be at any time. |
364 | be at any time. |
|
|
365 | |
|
|
366 | $current_pid = $w->pid |
|
|
367 | $old_pid = $w->pid ($new_pid) |
|
|
368 | Returns the previously set process id and optionally set a new one. |
|
|
369 | |
|
|
370 | $exit_status = $w->rstatus |
|
|
371 | Return the exit/wait status (as returned by waitpid, see the waitpid |
|
|
372 | entry in perlfunc). |
|
|
373 | |
|
|
374 | $pid = $w->rpid |
|
|
375 | Return the pid of the awaited child (useful when you have installed |
|
|
376 | a watcher for all pids). |
291 | |
377 | |
292 | $w = EV::idle $callback |
378 | $w = EV::idle $callback |
293 | $w = EV::idle_ns $callback |
379 | $w = EV::idle_ns $callback |
294 | Call the callback when there are no pending io, timer/periodic, |
380 | Call the callback when there are no pending io, timer/periodic, |
295 | signal or child events, i.e. when the process is idle. |
381 | signal or child events, i.e. when the process is idle. |
… | |
… | |
364 | THREADS |
450 | THREADS |
365 | Threads are not supported by this in any way. Perl pseudo-threads is |
451 | Threads are not supported by this in any way. Perl pseudo-threads is |
366 | evil stuff and must die. |
452 | evil stuff and must die. |
367 | |
453 | |
368 | SEE ALSO |
454 | SEE ALSO |
369 | L<EV::DNS>, L<EV::AnyEvent>. |
455 | L<EV::DNS>. |
370 | |
456 | |
371 | AUTHOR |
457 | AUTHOR |
372 | Marc Lehmann <schmorp@schmorp.de> |
458 | Marc Lehmann <schmorp@schmorp.de> |
373 | http://home.schmorp.de/ |
459 | http://home.schmorp.de/ |
374 | |
460 | |