… | |
… | |
540 | This is a special request - it does nothing in itself and is only used for |
540 | This is a special request - it does nothing in itself and is only used for |
541 | side effects, such as when you want to add a dummy request to a group so |
541 | side effects, such as when you want to add a dummy request to a group so |
542 | that finishing the requests in the group depends on executing the given |
542 | that finishing the requests in the group depends on executing the given |
543 | code. |
543 | code. |
544 | |
544 | |
|
|
545 | While this request does nothing, it still goes through the execution |
|
|
546 | phase and still requires a worker thread. Thus, the callback will not |
|
|
547 | be executed immediately but only after other requests in the queue have |
|
|
548 | entered their execution phase. This can be used to measure request |
|
|
549 | latency. |
|
|
550 | |
545 | =item IO::AIO::aio_sleep $fractional_seconds, $callback->() *NOT EXPORTED* |
551 | =item IO::AIO::aio_sleep $fractional_seconds, $callback->() *NOT EXPORTED* |
546 | |
552 | |
547 | Mainly used for debugging and benchmarking, this aio request puts one of |
553 | Mainly used for debugging and benchmarking, this aio request puts one of |
548 | the request workers to sleep for the given time. |
554 | the request workers to sleep for the given time. |
549 | |
555 | |
… | |
… | |
567 | callback) and B<done> (request has reached the end of its lifetime and |
573 | callback) and B<done> (request has reached the end of its lifetime and |
568 | holds no resources anymore). |
574 | holds no resources anymore). |
569 | |
575 | |
570 | =over 4 |
576 | =over 4 |
571 | |
577 | |
572 | =item $req->cancel |
578 | =item cancel $req |
573 | |
579 | |
574 | Cancels the request, if possible. Has the effect of skipping execution |
580 | Cancels the request, if possible. Has the effect of skipping execution |
575 | when entering the B<execute> state and skipping calling the callback when |
581 | when entering the B<execute> state and skipping calling the callback when |
576 | entering the the B<result> state, but will leave the request otherwise |
582 | entering the the B<result> state, but will leave the request otherwise |
577 | untouched. That means that requests that currently execute will not be |
583 | untouched. That means that requests that currently execute will not be |
578 | stopped and resources held by the request will not be freed prematurely. |
584 | stopped and resources held by the request will not be freed prematurely. |
579 | |
585 | |
|
|
586 | =item cb $req $callback->(...) |
|
|
587 | |
|
|
588 | Replace (or simply set) the callback registered to the request. |
|
|
589 | |
580 | =back |
590 | =back |
581 | |
591 | |
582 | =head2 IO::AIO::GRP CLASS |
592 | =head2 IO::AIO::GRP CLASS |
583 | |
593 | |
584 | This class is a subclass of L<IO::AIO::REQ>, so all its methods apply to |
594 | This class is a subclass of L<IO::AIO::REQ>, so all its methods apply to |
… | |
… | |
641 | group. And only when all those requests have finished will the the group |
651 | group. And only when all those requests have finished will the the group |
642 | itself finish. |
652 | itself finish. |
643 | |
653 | |
644 | =over 4 |
654 | =over 4 |
645 | |
655 | |
|
|
656 | =item add $grp ... |
|
|
657 | |
646 | =item $grp->add (...) |
658 | =item $grp->add (...) |
647 | |
|
|
648 | =item add $grp ... |
|
|
649 | |
659 | |
650 | Add one or more requests to the group. Any type of L<IO::AIO::REQ> can |
660 | Add one or more requests to the group. Any type of L<IO::AIO::REQ> can |
651 | be added, including other groups, as long as you do not create circular |
661 | be added, including other groups, as long as you do not create circular |
652 | dependencies. |
662 | dependencies. |
653 | |
663 | |
… | |
… | |
656 | =item $grp->result (...) |
666 | =item $grp->result (...) |
657 | |
667 | |
658 | Set the result value(s) that will be passed to the group callback when all |
668 | Set the result value(s) that will be passed to the group callback when all |
659 | subrequests have finished. By default, no argument will be passed. |
669 | subrequests have finished. By default, no argument will be passed. |
660 | |
670 | |
661 | =item $grp->set_feeder ($callback->($grp)) |
671 | =item feed $grp $callback->($grp) |
662 | |
672 | |
663 | [VERY EXPERIMENTAL] |
673 | [VERY EXPERIMENTAL] |
664 | |
674 | |
665 | Sets a feeder/generator on this group: every group can have an attached |
675 | Sets a feeder/generator on this group: every group can have an attached |
666 | generator that generates requests if idle. The idea behind this is that, |
676 | generator that generates requests if idle. The idea behind this is that, |
… | |
… | |
669 | example, C<aio_scandir> might generate hundreds of thousands C<aio_stat> |
679 | example, C<aio_scandir> might generate hundreds of thousands C<aio_stat> |
670 | requests, delaying any later requests for a long time. |
680 | requests, delaying any later requests for a long time. |
671 | |
681 | |
672 | To avoid this, and allow incremental generation of requests, you can |
682 | To avoid this, and allow incremental generation of requests, you can |
673 | instead a group and set a feeder on it that generates those requests. The |
683 | instead a group and set a feeder on it that generates those requests. The |
674 | feeder will be called whenever there are few enough (see C<feeder_limit>, |
684 | feed callback will be called whenever there are few enough (see C<feed_limit>, |
675 | below) requests active in the group itself and is expected to queue more |
685 | below) requests active in the group itself and is expected to queue more |
676 | requests. |
686 | requests. |
677 | |
687 | |
678 | The feeder can queue as many requests as it likes (i.e. C<add> does not |
688 | The feed can queue as many requests as it likes (i.e. C<add> does not |
679 | impose any limits). |
689 | impose any limits). |
680 | |
690 | |
681 | If the feeder does not queue more requests when called, it will be |
691 | If the feed does not queue more requests when called, it will be |
682 | automatically removed from the group. |
692 | automatically removed from the group. |
683 | |
693 | |
684 | If the feeder limit is C<0>, it will be set to C<2> automatically. |
694 | If the feed limit is C<0>, it will be set to C<2> automatically. |
685 | |
695 | |
686 | Example: |
696 | Example: |
687 | |
697 | |
688 | # stat all files in @files, but only ever use four aio requests concurrently: |
698 | # stat all files in @files, but only ever use four aio requests concurrently: |
689 | |
699 | |
690 | my $grp = aio_group sub { print "finished\n" }; |
700 | my $grp = aio_group sub { print "finished\n" }; |
691 | $grp->feeder_limit (4); |
701 | feed_limit $grp 4; |
692 | $grp->set_feeder (sub { |
702 | feed $grp sub { |
693 | my $file = pop @files |
703 | my $file = pop @files |
694 | or return; |
704 | or return; |
695 | |
705 | |
696 | add $grp aio_stat $file, sub { ... }; |
706 | add $grp aio_stat $file, sub { ... }; |
697 | }); |
707 | }; |
698 | |
708 | |
699 | =item $grp->feeder_limit ($num) |
709 | =item feed_limit $grp $num |
700 | |
710 | |
701 | Sets the feeder limit for the group: The feeder will be called whenever |
711 | Sets the feeder limit for the group: The feeder will be called whenever |
702 | the group contains less than this many requests. |
712 | the group contains less than this many requests. |
703 | |
713 | |
704 | Setting the limit to C<0> will pause the feeding process. |
714 | Setting the limit to C<0> will pause the feeding process. |