logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

AnyEvent::ForkObject - Async access on objects.

Author

       Dmitry E. Oboukhov, <unera@debian.org>

Description

       There are a lot of modules that provide object interface. Using the module You can use them in async
       mode.

Methods

new
       Constructor. Creates an instance that contains fork jail.

   do
       Creates an object inside jail. It receives the following named arguments:

       require
           Do  require  inside  jail.  If the argument is exists, module, method and wantarray arguments will be
           ignored.

       module
           Module name. For example 'DBI'.

       method
           Constructor name. Default value is 'new'.

       wantarray
           Context for method. Default is 0 (SCALAR).

       cb  Done callback. The first argument is a status:

           die The method has thrown exception. The next argument contains $@.

           fatal
               A fatal error was occured (for example fork jail was killed).

           ok  Method has done. The following arguments contain all data that were returned by the method.

       If "method" returns blessed object, it will provide all its methods in modified form.  Each  method  will
       receive one or two additional arguments:

       resultcallback
           A callback that will be called after method has done.

       wantarray
           Context flag for method. Default value is 0 (SCALAR).

       All objects provide additional method fo_attr to access their field.  Example:

           # set attribute
           $dbh->fo_attr(RaiseError => 1, sub { my ($status, $attr) = @_; ... });

           # get attribute
           $dbh->fo_attr('RaiseError', sub { my ($status, $attr) = @_; ... });

Name

       AnyEvent::ForkObject - Async access on objects.

Synopsis

           use AnyEvent::ForkObject;
           use DBI;

           my $fo = new AnyEvent::ForkObject;

           $fo->do(
               module => 'DBI',
               method => 'connect',
               args => [ 'dbi:mysql...' ],
               cb => sub {
                   my ($status, $dbh) = @_;

                   $dbh->selectrow_array('SELECT ?', undef, 1 + 1, sub {
                       my ($status, $result) = @_;
                       print "$result\n";   # prints 2
                   });
               }
           );

           use AnyEvent::Tools qw(async_repeat);

           $dbh->prepare('SELECT * FROM tbl', sub {
               my ($status, $sth) = @_;
               $sth->execute(sub {
                   my ($status, $rv) = @_;

                   # fetch 30 rows
                   async_repeat 30 => sub {
                       my ($guard) = @_;

                       $sth->fetchrow_hashref(sub {
                           my ($status, $row) = @_;
                           undef $guard;

                           # do something with $row
                       });
                   };

               });
           });

Vcs

       The project is placed in my git repo: <http://git.uvw.ru/?p=anyevent-forkobject;a=summary>

perl v5.40.1                                       2025-05-27                          AnyEvent::ForkObject(3pm)

See Also