… | |
… | |
79 | our $VERSION = '2.0'; |
79 | our $VERSION = '2.0'; |
80 | |
80 | |
81 | our @EXPORT = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
81 | our @EXPORT = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
82 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
82 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
83 | aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move |
83 | aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move |
84 | aio_group); |
84 | aio_group aio_nop); |
85 | our @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel max_outstanding nreqs); |
85 | our @EXPORT_OK = qw(poll_fileno poll_cb min_parallel max_parallel max_outstanding nreqs); |
86 | |
86 | |
87 | @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; |
87 | @IO::AIO::GRP::ISA = 'IO::AIO::REQ'; |
88 | |
88 | |
89 | require XSLoader; |
89 | require XSLoader; |
… | |
… | |
533 | add $grp |
533 | add $grp |
534 | (aio_stat ...), |
534 | (aio_stat ...), |
535 | (aio_stat ...), |
535 | (aio_stat ...), |
536 | ...; |
536 | ...; |
537 | |
537 | |
|
|
538 | =item aio_nop $callback->() |
|
|
539 | |
|
|
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 |
|
|
542 | that finishing the requests in the group depends on executing the given |
|
|
543 | code. |
|
|
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 | |
538 | =item IO::AIO::aio_sleep $fractional_seconds, $callback->() *NOT EXPORTED* |
551 | =item IO::AIO::aio_sleep $fractional_seconds, $callback->() *NOT EXPORTED* |
539 | |
552 | |
540 | 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 |
541 | the request workers to sleep for the given time. |
554 | the request workers to sleep for the given time. |
542 | |
555 | |
… | |
… | |
560 | 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 |
561 | holds no resources anymore). |
574 | holds no resources anymore). |
562 | |
575 | |
563 | =over 4 |
576 | =over 4 |
564 | |
577 | |
565 | =item $req->cancel |
578 | =item cancel $req |
566 | |
579 | |
567 | Cancels the request, if possible. Has the effect of skipping execution |
580 | Cancels the request, if possible. Has the effect of skipping execution |
568 | 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 |
569 | 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 |
570 | untouched. That means that requests that currently execute will not be |
583 | untouched. That means that requests that currently execute will not be |
571 | 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. |
572 | |
585 | |
|
|
586 | =item cb $req $callback->(...) |
|
|
587 | |
|
|
588 | Replace (or simply set) the callback registered to the request. |
|
|
589 | |
573 | =back |
590 | =back |
574 | |
591 | |
575 | =head2 IO::AIO::GRP CLASS |
592 | =head2 IO::AIO::GRP CLASS |
576 | |
593 | |
577 | 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 |
… | |
… | |
634 | 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 |
635 | itself finish. |
652 | itself finish. |
636 | |
653 | |
637 | =over 4 |
654 | =over 4 |
638 | |
655 | |
|
|
656 | =item add $grp ... |
|
|
657 | |
639 | =item $grp->add (...) |
658 | =item $grp->add (...) |
640 | |
|
|
641 | =item add $grp ... |
|
|
642 | |
659 | |
643 | 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 |
644 | 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 |
645 | dependencies. |
662 | dependencies. |
646 | |
663 | |
… | |
… | |
649 | =item $grp->result (...) |
666 | =item $grp->result (...) |
650 | |
667 | |
651 | 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 |
652 | subrequests have finished. By default, no argument will be passed. |
669 | subrequests have finished. By default, no argument will be passed. |
653 | |
670 | |
654 | =item $grp->set_feeder ($callback->($grp)) |
671 | =item feed $grp $callback->($grp) |
655 | |
672 | |
656 | [VERY EXPERIMENTAL] |
673 | [VERY EXPERIMENTAL] |
657 | |
674 | |
658 | 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 |
659 | 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, |
… | |
… | |
662 | 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> |
663 | requests, delaying any later requests for a long time. |
680 | requests, delaying any later requests for a long time. |
664 | |
681 | |
665 | To avoid this, and allow incremental generation of requests, you can |
682 | To avoid this, and allow incremental generation of requests, you can |
666 | 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 |
667 | 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>, |
668 | 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 |
669 | requests. |
686 | requests. |
670 | |
687 | |
671 | 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 |
672 | impose any limits). |
689 | impose any limits). |
673 | |
690 | |
674 | 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 |
675 | automatically removed from the group. |
692 | automatically removed from the group. |
676 | |
693 | |
677 | 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. |
678 | |
695 | |
679 | Example: |
696 | Example: |
680 | |
697 | |
681 | # 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: |
682 | |
699 | |
683 | my $grp = aio_group sub { print "finished\n" }; |
700 | my $grp = aio_group sub { print "finished\n" }; |
684 | $grp->feeder_limit (4); |
701 | feed_limit $grp 4; |
685 | $grp->set_feeder (sub { |
702 | feed $grp sub { |
686 | my $file = pop @files |
703 | my $file = pop @files |
687 | or return; |
704 | or return; |
688 | |
705 | |
689 | add $grp aio_stat $file, sub { ... }; |
706 | add $grp aio_stat $file, sub { ... }; |
690 | }); |
707 | }; |
691 | |
708 | |
692 | =item $grp->feeder_limit ($num) |
709 | =item feed_limit $grp $num |
693 | |
710 | |
694 | 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 |
695 | the group contains less than this many requests. |
712 | the group contains less than this many requests. |
696 | |
713 | |
697 | Setting the limit to C<0> will pause the feeding process. |
714 | Setting the limit to C<0> will pause the feeding process. |