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

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.142 by root, Tue May 27 02:34:30 2008 UTC vs.
Revision 1.143 by root, Wed May 28 23:57:38 2008 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines