Coro

 view release on metacpan or  search on metacpan

Coro/Intro.pod  view on Meta::CPAN

   exit                    end this session

Microsft victims can of course use the even less secure C<new_tcp_server>
constructor.


=head2 The Real World - File I/O

Disk I/O, while often much faster than the network, nevertheless can take
quite a long time in which the CPU could do other things, if one would
only be able to do something.

Fortunately, the L<IO::AIO> module on CPAN allows you to move these
I/O calls into the background, letting you do useful work in the
foreground. It is event-/callback-based, but Coro has a nice wrapper
around it, called L<Coro::AIO>, which lets you use its functions
naturally from within threads:

   use Fcntl;
   use Coro::AIO;

   my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600
      or die "$filename~: $!";

   aio_write $fh, 0, (length $data), $data, 0;
   aio_fsync $fh;
   aio_close $fh;
   aio_rename "$filename~", "$filename";

The above creates a new file, writes data into it, syncs the data to disk
and atomically replaces a base file with a new copy.


=head2 Inversion of control - rouse functions

Last not least, me talk about inversion of control. The "control" refers
to "who calls whom", who is in control of the program. In this program,
the main program is in control and passes this to all functions it calls:

   use LWP;

   # pass control to get
   my $res = get "http://example.org/";
   # control returned to us

   print $res;

When switching to event-based programs, instead of "us calling them",
"they call us" - this is the inversion of control form the title:

   use AnyEvent::HTTP;

   # do not pass control for long - http_get immediately returns
   http_get "http://example.org/", sub {
      print $_[0];
   };

   # we stay in control and can do other things

Event based programming can be nice, but sometimes it's just easier to
write down some processing in "linear" fashion, without callbacks. Coro
provides some special functions to reduce typing:

   use AnyEvent::HTTP;

   # do not pass control for long - http_get immediately returns
   http_get "http://example.org/", Coro::rouse_cb;

   # we stay in control and can do other things...
   # ...such as wait for the result
   my ($res) = Coro::rouse_wait;

C<Coro::rouse_cb> creates and returns a special callback. You can pass
this callback to any function that would expect a callback.

C<Coro::rouse_wait> waits (block the current thread) until the most
recently created callback has been called, and returns whatever was passed
to it.

These two functions allow you to I<mechanically> invert the control from
"callback based style" used by most event-based libraries to "blocking
style", whenever you wish to.

The pattern is simple: instead of...

   some_func ..., sub {
      my @res = @_;
      ...
   };

... you write:

   some_func ..., Coro::rouse_cb;
   my @res = Coro::rouse_wait;
   ...

Callback-based interfaces are plenty, and the rouse functions allow you to
use them in an often more convenient way.


=head1 Other Modules

This introduction only mentions a few methods and modules, Coro has many
other functions (see the L<Coro> manpage) and modules (documented in the
C<SEE ALSO> section of the L<Coro> manpage).

Noteworthy modules are L<Coro::LWP> (for parallel LWP requests, but see
L<AnyEvent::HTTP> for a better HTTP-only alternative), L<Coro::BDB>, for
when you need an asynchronous database, L<Coro::Handle>, when you need
to use any file handle in a coroutine (popular to access C<STDIN> and
C<STDOUT>) and L<Coro::EV>, the optimised interface to L<EV> (which gets
used automatically by L<Coro::AnyEvent>).

There are a number of Coro-related moduels that might be useful for your problem
(see L<http://search.cpan.org/search?query=Coro&mode=module>). And since Coro
integrates so well into AnyEvent, it's often easy to adapt existing AnyEvent modules
(see L<http://search.cpan.org/search?query=AnyEvent&mode=module>).


=head1 AUTHOR



( run in 0.465 second using v1.01-cache-2.11-cpan-39bf76dae61 )