Last active
March 3, 2023 03:33
-
-
Save s1037989/d9c03050b22fedd2057e43869b8eeb82 to your computer and use it in GitHub Desktop.
Mojo::Promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Need a separate process to block to show what happens when a waiting for a non-blocking op | |
| # 12 workers that each handle only 1 concurrent connection so that each connection can immediately be fulfilled and start the countdown | |
| $ perl -Mojo -MTime::HiRes=time,sleep -E 'a("/#n" => sub { my $time = time; my $n = $_->param("n"); sleep $n; $_->render(inline => "<title>$n : Sleep <%= \$n %> seconds</title>", n => time - $time) })->start' prefork -w 12 -c 1 | |
| Web application available at http://127.0.0.1:3000 | |
| --- | |
| $ perl promise.pl 1 http://localhost:3000/4 http://localhost:3000/3 http://localhost:3000/2 http://localhost:3000/5 http://localhost:3000/6 http://localhost:3000/7 | |
| Getting 6 URLs starting at 1677811334.05866 | |
| Getting http://localhost:3000/6 starting after 3.00407409667969e-05 | |
| Getting http://localhost:3000/2 starting after 6.01262807846069 | |
| Getting http://localhost:3000/5 starting after 8.0180242061615 | |
| Getting http://localhost:3000/3 starting after 13.0254700183868 | |
| Getting http://localhost:3000/7 starting after 16.0332491397858 | |
| Getting http://localhost:3000/4 starting after 23.0408351421356 | |
| Done getting 6 URLs after 27.048406124115 | |
| Done getting 6 URLs after 27.0521461963654 | |
| Titles: | |
| 6 : Sleep 6.00025391578674 seconds | |
| 2 : Sleep 2.00024104118347 seconds | |
| 5 : Sleep 5.00025606155396 seconds | |
| 3 : Sleep 3.0002429485321 seconds | |
| 7 : Sleep 7.00025081634521 seconds | |
| 4 : Sleep 4.00020003318787 seconds | |
| $ perl promise.pl 7 http://localhost:3000/4 http://localhost:3000/3 http://localhost:3000/2 http://localhost:3000/5 http://localhost:3000/6 http://localhost:3000/7 | |
| Getting 6 URLs starting at 1677811389.73466 | |
| Getting http://localhost:3000/5 starting after 4.69684600830078e-05 | |
| Getting http://localhost:3000/3 starting after 0.000531196594238281 | |
| Getting http://localhost:3000/6 starting after 0.00151801109313965 | |
| Getting http://localhost:3000/2 starting after 0.00247716903686523 | |
| Getting http://localhost:3000/7 starting after 0.0038151741027832 | |
| Getting http://localhost:3000/4 starting after 0.00556612014770508 | |
| Done getting 6 URLs after 7.02463006973267 | |
| Done getting 6 URLs after 7.02584218978882 | |
| Titles: | |
| 5 : Sleep 5.00024104118347 seconds | |
| 3 : Sleep 3.00025796890259 seconds | |
| 6 : Sleep 6.00024700164795 seconds | |
| 2 : Sleep 2.00023198127747 seconds | |
| 7 : Sleep 7.00027203559875 seconds | |
| 4 : Sleep 4.00024795532227 seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ perl promise.pl 1 5 | |
| Getting 5 URLs starting at 1677814112.0623 | |
| Getting 2.67349671630221 starting after 3.19480895996094e-05 | |
| Getting 7.58055475027298 starting after 2.67741799354553 | |
| Getting 7.06455020618088 starting after 10.2669320106506 | |
| Getting 4.69123169999499 starting after 17.3339018821716 | |
| Getting 4.97515645843897 starting after 22.0288639068604 | |
| Done getting 5 URLs after 27.0098218917847 | |
| Done getting 5 URLs after 27.0103900432587 | |
| Titles: | |
| 2.67349671630221 : Sleep 2.67349671630221 seconds | |
| 7.58055475027298 : Sleep 7.58055475027298 seconds | |
| 7.06455020618088 : Sleep 7.06455020618088 seconds | |
| 4.69123169999499 : Sleep 4.69123169999499 seconds | |
| 4.97515645843897 : Sleep 4.97515645843897 seconds | |
| $ perl promise.pl 5 5 | |
| Getting 5 URLs starting at 1677814146.18375 | |
| Getting 0.862842012308857 starting after 3.38554382324219e-05 | |
| Getting 7.91875325876254 starting after 0.000115871429443359 | |
| Getting 1.59643112482432 starting after 0.000144004821777344 | |
| Getting 7.22928301396243 starting after 0.000169038772583008 | |
| Getting 8.12815142049452 starting after 0.000187873840332031 | |
| Done getting 5 URLs after 8.12967205047607 | |
| Done getting 5 URLs after 8.13023591041565 | |
| Titles: | |
| 0.862842012308857 : Sleep 0.862842012308857 seconds | |
| 7.91875325876254 : Sleep 7.91875325876254 seconds | |
| 1.59643112482432 : Sleep 1.59643112482432 seconds | |
| 7.22928301396243 : Sleep 7.22928301396243 seconds | |
| 8.12815142049452 : Sleep 8.12815142049452 seconds | |
| $ cat promise.pl | |
| use Mojo::Base -strict, -signatures; | |
| use Mojo::Collection qw(c); | |
| use Mojo::Promise; | |
| use Time::HiRes qw(time); | |
| my $ua = Mojo::UserAgent->new; | |
| my $cc = shift @ARGV; | |
| my $urls = c(map { rand(9) } 1..$ARGV[0])->shuffle; | |
| my @titles; | |
| my $time = time; | |
| warn sprintf "Getting %d URLs starting at %s\n", $urls->size, $time; | |
| Mojo::Promise->map({concurrency => $cc}, sub { | |
| warn sprintf "Getting %s starting after %s\n", $_, time - $time; | |
| Mojo::Promise->timer($_ => "$_ : Sleep $_ seconds"); | |
| }, @$urls)->then(sub{ | |
| warn sprintf "Done getting %d URLs after %s\n", $urls->size, time - $time; | |
| @titles = map { $_->[0] } @_ | |
| })->wait; | |
| warn sprintf "Done getting %d URLs after %s\n", $urls->size, time - $time; | |
| warn "Titles:\n"; | |
| say join "\n", map { " $_" } @titles; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use Mojo::Base -strict, -signatures; | |
| use Mojo::Collection qw(c); | |
| use Mojo::IOLoop; | |
| use Mojo::Promise; | |
| use Mojo::UserAgent; | |
| use Time::HiRes qw(time); | |
| my $ua = Mojo::UserAgent->new; | |
| my $cc = shift @ARGV; | |
| my $urls = c(qw(https://mojolicious.org https://metacpan.org), @ARGV)->shuffle; | |
| my @titles; | |
| my $time = time; | |
| warn sprintf "Getting %d URLs starting at %s\n", $urls->size, $time; | |
| Mojo::Promise->map({concurrency => $cc}, sub { | |
| warn sprintf "Getting %s starting after %s\n", $_, time - $time; | |
| $ua->get_p($_) | |
| }, @$urls)->then(sub{ | |
| warn sprintf "Done getting %d URLs after %s\n", $urls->size, time - $time; | |
| @titles = map { $_->[0]->res->dom->at('title')->text } @_ | |
| })->wait; | |
| warn sprintf "Done getting %d URLs after %s\n", $urls->size, time - $time; | |
| warn "Titles:\n"; | |
| say join "\n", map { " $_" } @titles; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment