Created
April 20, 2020 23:10
-
-
Save rmallah/fcb716361c628ec3339e238cfb778d17 to your computer and use it in GitHub Desktop.
This file contains 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
sub get_p { | |
my ($c) = shift; | |
$c->log->debug("creating a promise"); | |
my $promise = Mojo::Promise->new ; | |
$c->subprocess(sub { | |
my $n = int (3 + rand 10) ; | |
for (1..$n) { | |
$c->log->debug("I am $$, will sleep for $n seconds."); | |
sleep 1 ; | |
} | |
return $n; | |
}, sub { | |
my ($c, @results) = @_; | |
$promise->resolve ($results[0]) ; | |
}); | |
$c->log->debug("returning the promise"); | |
return $promise; | |
} | |
sub test_subprocess { | |
my ($c) = shift; | |
$c->log->debug("I am in main process : $$"); | |
my (@promises) = map { get_p ($c) } (1..5); | |
Mojo::Promise->all(@promises)->then(sub { | |
my (@all) = @_; | |
$c->log->debug("all promises are settled."); | |
$c->render(text => "All promises handled"); | |
})->catch(sub { | |
my $err = shift; | |
$c->log->warn("Something went wrong: $err") ; | |
})->wait; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment