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.284 by root, Sun Aug 16 16:54:51 2009 UTC vs.
Revision 1.296 by root, Tue Nov 17 01:19:49 2009 UTC

362this "current" time will differ substantially from the real time, which 362this "current" time will differ substantially from the real time, which
363might affect timers and time-outs. 363might affect timers and time-outs.
364 364
365When this is the case, you can call this method, which will update the 365When this is the case, you can call this method, which will update the
366event loop's idea of "current time". 366event loop's idea of "current time".
367
368A typical example would be a script in a web server (e.g. C<mod_perl>) -
369when mod_perl executes the script, then the event loop will have the wrong
370idea about the "current time" (being potentially far in the past, when the
371script ran the last time). In that case you should arrange a call to C<<
372AnyEvent->now_update >> each time the web server process wakes up again
373(e.g. at the start of your script, or in a handler).
367 374
368Note that updating the time I<might> cause some events to be handled. 375Note that updating the time I<might> cause some events to be handled.
369 376
370=back 377=back
371 378
592 after => 1, 599 after => 1,
593 cb => sub { $result_ready->send }, 600 cb => sub { $result_ready->send },
594 ); 601 );
595 602
596 # this "blocks" (while handling events) till the callback 603 # this "blocks" (while handling events) till the callback
597 # calls -<send 604 # calls ->send
598 $result_ready->recv; 605 $result_ready->recv;
599 606
600Example: wait for a timer, but take advantage of the fact that condition 607Example: wait for a timer, but take advantage of the fact that condition
601variables are also callable directly. 608variables are also callable directly.
602 609
1106 1113
1107package AnyEvent; 1114package AnyEvent;
1108 1115
1109# basically a tuned-down version of common::sense 1116# basically a tuned-down version of common::sense
1110sub common_sense { 1117sub common_sense {
1111 # no warnings 1118 # from common:.sense 1.0
1112 ${^WARNING_BITS} ^= ${^WARNING_BITS}; 1119 ${^WARNING_BITS} = "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
1113 # use strict vars subs 1120 # use strict vars subs
1114 $^H |= 0x00000600; 1121 $^H |= 0x00000600;
1115} 1122}
1116 1123
1117BEGIN { AnyEvent::common_sense } 1124BEGIN { AnyEvent::common_sense }
1118 1125
1119use Carp (); 1126use Carp ();
1120 1127
1121our $VERSION = '5.111'; 1128our $VERSION = '5.202';
1122our $MODEL; 1129our $MODEL;
1123 1130
1124our $AUTOLOAD; 1131our $AUTOLOAD;
1125our @ISA; 1132our @ISA;
1126 1133
1343 1350
1344package AnyEvent::Base; 1351package AnyEvent::Base;
1345 1352
1346# default implementations for many methods 1353# default implementations for many methods
1347 1354
1348sub _time { 1355sub _time() {
1349 # probe for availability of Time::HiRes 1356 # probe for availability of Time::HiRes
1350 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1357 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1351 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; 1358 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8;
1352 *_time = \&Time::HiRes::time; 1359 *_time = \&Time::HiRes::time;
1353 # if (eval "use POSIX (); (POSIX::times())... 1360 # if (eval "use POSIX (); (POSIX::times())...
1373 1380
1374our $HAVE_ASYNC_INTERRUPT; 1381our $HAVE_ASYNC_INTERRUPT;
1375 1382
1376sub _have_async_interrupt() { 1383sub _have_async_interrupt() {
1377 $HAVE_ASYNC_INTERRUPT = 1*(!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} 1384 $HAVE_ASYNC_INTERRUPT = 1*(!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT}
1378 && eval "use Async::Interrupt 1.0 (); 1") 1385 && eval "use Async::Interrupt 1.02 (); 1")
1379 unless defined $HAVE_ASYNC_INTERRUPT; 1386 unless defined $HAVE_ASYNC_INTERRUPT;
1380 1387
1381 $HAVE_ASYNC_INTERRUPT 1388 $HAVE_ASYNC_INTERRUPT
1382} 1389}
1383 1390
1386our ($SIG_COUNT, $SIG_TW); 1393our ($SIG_COUNT, $SIG_TW);
1387 1394
1388sub _signal_exec { 1395sub _signal_exec {
1389 $HAVE_ASYNC_INTERRUPT 1396 $HAVE_ASYNC_INTERRUPT
1390 ? $SIGPIPE_R->drain 1397 ? $SIGPIPE_R->drain
1391 : sysread $SIGPIPE_R, my $dummy, 9; 1398 : sysread $SIGPIPE_R, (my $dummy), 9;
1392 1399
1393 while (%SIG_EV) { 1400 while (%SIG_EV) {
1394 for (keys %SIG_EV) { 1401 for (keys %SIG_EV) {
1395 delete $SIG_EV{$_}; 1402 delete $SIG_EV{$_};
1396 $_->() for values %{ $SIG_CB{$_} || {} }; 1403 $_->() for values %{ $SIG_CB{$_} || {} };
1912 warn "read: $input\n"; # output what has been read 1919 warn "read: $input\n"; # output what has been read
1913 $cv->send if $input =~ /^q/i; # quit program if /^q/i 1920 $cv->send if $input =~ /^q/i; # quit program if /^q/i
1914 }, 1921 },
1915 ); 1922 );
1916 1923
1917 my $time_watcher; # can only be used once
1918
1919 sub new_timer {
1920 $timer = AnyEvent->timer (after => 1, cb => sub { 1924 my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub {
1921 warn "timeout\n"; # print 'timeout' about every second 1925 warn "timeout\n"; # print 'timeout' at most every second
1922 &new_timer; # and restart the time
1923 }); 1926 });
1924 }
1925
1926 new_timer; # create first timer
1927 1927
1928 $cv->recv; # wait until user enters /^q/i 1928 $cv->recv; # wait until user enters /^q/i
1929 1929
1930=head1 REAL-WORLD EXAMPLE 1930=head1 REAL-WORLD EXAMPLE
1931 1931
2375As you can see, the AnyEvent + EV combination even beats the 2375As you can see, the AnyEvent + EV combination even beats the
2376hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl 2376hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl
2377backend easily beats IO::Lambda and POE. 2377backend easily beats IO::Lambda and POE.
2378 2378
2379And even the 100% non-blocking version written using the high-level (and 2379And even the 100% non-blocking version written using the high-level (and
2380slow :) L<AnyEvent::Handle> abstraction beats both POE and IO::Lambda by a 2380slow :) L<AnyEvent::Handle> abstraction beats both POE and IO::Lambda
2381large margin, even though it does all of DNS, tcp-connect and socket I/O 2381higher level ("unoptimised") abstractions by a large margin, even though
2382in a non-blocking way. 2382it does all of DNS, tcp-connect and socket I/O in a non-blocking way.
2383 2383
2384The two AnyEvent benchmarks programs can be found as F<eg/ae0.pl> and 2384The two AnyEvent benchmarks programs can be found as F<eg/ae0.pl> and
2385F<eg/ae2.pl> in the AnyEvent distribution, the remaining benchmarks are 2385F<eg/ae2.pl> in the AnyEvent distribution, the remaining benchmarks are
2386part of the IO::lambda distribution and were used without any changes. 2386part of the IO::Lambda distribution and were used without any changes.
2387 2387
2388 2388
2389=head1 SIGNALS 2389=head1 SIGNALS
2390 2390
2391AnyEvent currently installs handlers for these signals: 2391AnyEvent currently installs handlers for these signals:
2480lot less memory), but otherwise doesn't affect guard operation much. It is 2480lot less memory), but otherwise doesn't affect guard operation much. It is
2481purely used for performance. 2481purely used for performance.
2482 2482
2483=item L<JSON> and L<JSON::XS> 2483=item L<JSON> and L<JSON::XS>
2484 2484
2485This module is required when you want to read or write JSON data via 2485One of these modules is required when you want to read or write JSON data
2486L<AnyEvent::Handle>. It is also written in pure-perl, but can take 2486via L<AnyEvent::Handle>. It is also written in pure-perl, but can take
2487advantage of the ultra-high-speed L<JSON::XS> module when it is installed. 2487advantage of the ultra-high-speed L<JSON::XS> module when it is installed.
2488 2488
2489In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is 2489In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is
2490installed. 2490installed.
2491 2491

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines