… | |
… | |
232 | on true relative time) and absolute (ev_periodic, based on wallclock time) |
232 | on true relative time) and absolute (ev_periodic, based on wallclock time) |
233 | timers. |
233 | timers. |
234 | |
234 | |
235 | AnyEvent always prefers relative timers, if available, matching the |
235 | AnyEvent always prefers relative timers, if available, matching the |
236 | AnyEvent API. |
236 | AnyEvent API. |
|
|
237 | |
|
|
238 | AnyEvent has two additional methods that return the "current time": |
|
|
239 | |
|
|
240 | =over 4 |
|
|
241 | |
|
|
242 | =item AnyEvent->time |
|
|
243 | |
|
|
244 | This returns the "current wallclock time" as a fractional number of |
|
|
245 | seconds since the Epoch (the same thing as C<time> or C<Time::HiRes::time> |
|
|
246 | return, and the result is guaranteed to be compatible with those). |
|
|
247 | |
|
|
248 | It progresses independently of any event loop processing. |
|
|
249 | |
|
|
250 | In almost all cases (in all cases if you don't care), this is the function |
|
|
251 | to call when you want to know the current time. |
|
|
252 | |
|
|
253 | =item AnyEvent->now |
|
|
254 | |
|
|
255 | This also returns the "current wallclock time", but unlike C<time>, above, |
|
|
256 | this value might change only once per event loop iteration, depending on |
|
|
257 | the event loop (most return the same time as C<time>, above). This is the |
|
|
258 | time that AnyEvent timers get scheduled against. |
|
|
259 | |
|
|
260 | For a practical example of when these times differ, consider L<Event::Lib> |
|
|
261 | and L<EV> and the following set-up: |
|
|
262 | |
|
|
263 | The event loop is running and has just invoked one of your callback at |
|
|
264 | time=500 (assume no other callbacks delay processing). In your callback, |
|
|
265 | you wait a second by executing C<sleep 1> (blocking the process for a |
|
|
266 | second) and then (at time=501) you create a relative timer that fires |
|
|
267 | after three seconds. |
|
|
268 | |
|
|
269 | With L<Event::Lib>, C<< AnyEvent->time >> and C<< AnyEvent->now >> will |
|
|
270 | both return C<501>, because that is the current time, and the timer will |
|
|
271 | be scheduled to fire at time=504 (C<501> + C<3>). |
|
|
272 | |
|
|
273 | With L<EV>m C<< AnyEvent->time >> returns C<501> (as that is the current |
|
|
274 | time), but C<< AnyEvent->now >> returns C<500>, as that is the time the |
|
|
275 | last event processing phase started. With L<EV>, your timer gets scheduled |
|
|
276 | to run at time=503 (C<500> + C<3>). |
|
|
277 | |
|
|
278 | In one sense, L<Event::Lib> is more exact, as it uses the current time |
|
|
279 | regardless of any delays introduced by event processing. However, most |
|
|
280 | callbacks do not expect large delays in processing, so this causes a |
|
|
281 | higher drift (and a lot more syscalls to get the current time). |
|
|
282 | |
|
|
283 | In another sense, L<EV> is more exact, as your timer will be scheduled at |
|
|
284 | the same time, regardless of how long event processing actually took. |
|
|
285 | |
|
|
286 | In either case, if you care (and in most cases, you don't), then you |
|
|
287 | can get whatever behaviour you want with any event loop, by taking the |
|
|
288 | difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into |
|
|
289 | account. |
|
|
290 | |
|
|
291 | =back |
237 | |
292 | |
238 | =head2 SIGNAL WATCHERS |
293 | =head2 SIGNAL WATCHERS |
239 | |
294 | |
240 | You can watch for signals using a signal watcher, C<signal> is the signal |
295 | You can watch for signals using a signal watcher, C<signal> is the signal |
241 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
296 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
… | |
… | |
779 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
834 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
780 | [Wx:: => AnyEvent::Impl::POE::], |
835 | [Wx:: => AnyEvent::Impl::POE::], |
781 | [Prima:: => AnyEvent::Impl::POE::], |
836 | [Prima:: => AnyEvent::Impl::POE::], |
782 | ); |
837 | ); |
783 | |
838 | |
784 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
839 | our %method = map +($_ => 1), qw(io timer time now signal child condvar one_event DESTROY); |
785 | |
840 | |
786 | our @post_detect; |
841 | our @post_detect; |
787 | |
842 | |
788 | sub post_detect(&) { |
843 | sub post_detect(&) { |
789 | my ($cb) = @_; |
844 | my ($cb) = @_; |
… | |
… | |
873 | $class->$func (@_); |
928 | $class->$func (@_); |
874 | } |
929 | } |
875 | |
930 | |
876 | package AnyEvent::Base; |
931 | package AnyEvent::Base; |
877 | |
932 | |
|
|
933 | # default implementation for now and time |
|
|
934 | |
|
|
935 | use Time::HiRes (); |
|
|
936 | |
|
|
937 | sub time { Time::HiRes::time } |
|
|
938 | sub now { Time::HiRes::time } |
|
|
939 | |
878 | # default implementation for ->condvar |
940 | # default implementation for ->condvar |
879 | |
941 | |
880 | sub condvar { |
942 | sub condvar { |
881 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
943 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
882 | } |
944 | } |