Created
November 1, 2019 21:54
-
-
Save renatocron/08f74945a803bffec5042e09713d609a to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env perl | |
use common::sense; | |
use DDP; | |
use Mojo::Promise; | |
use Mojo::IOLoop::Subprocess; | |
use Mojo::UserAgent; | |
my $subprocess = Mojo::IOLoop::Subprocess->new; | |
$subprocess->run( | |
sub { | |
my $subprocess = shift; | |
my $ua = Mojo::UserAgent->new->inactivity_timeout(1)->connect_timeout(1); | |
my @promises; | |
for (1 .. 5) { | |
my $request_p = $ua->get_p(('https://perlcon.eu/')); | |
$request_p->then( | |
sub { | |
my ($tx) = @_; | |
print $tx->res->code . "\n"; | |
} | |
)->catch( | |
sub { | |
print STDERR @_; | |
} | |
); | |
push @promises, $request_p; | |
} | |
print gmtime . ": Before Mojo::Promise->all()->wait\n"; | |
Mojo::Promise->all(@promises)->wait(); | |
print gmtime . ": After Mojo::Promise->all()->wait\n"; | |
return 1; | |
}, | |
sub { | |
print "Done!!!\n\n\n"; | |
} | |
); | |
$subprocess->ioloop->start unless $subprocess->ioloop->is_running; | |
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
#!/usr/bin/env perl | |
use common::sense; | |
use DDP; | |
use Mojo::Promise; | |
use Mojo::IOLoop::Subprocess; | |
my $subprocess = Mojo::IOLoop::Subprocess->new; | |
$subprocess->run( | |
sub { | |
my $subprocess = shift; | |
use DDP; | |
my $new_ioloop = Mojo::IOLoop->new; | |
my @promises; | |
for (1 .. 10) { | |
my $p = Mojo::Promise->new()->ioloop($new_ioloop); | |
$p->then( | |
sub { | |
print STDERR "p is running\n\n"; | |
} | |
)->catch(sub { use DDP; p \@_; }); | |
push @promises, $p; | |
$new_ioloop->timer( | |
0.1 => sub { | |
$p->resolve('Lucky!'); | |
} | |
); | |
} | |
print gmtime . ": Before Mojo::Promise->all()->wait\n"; | |
Mojo::Promise->all(@promises)->wait(); | |
print gmtime . ": After Mojo::Promise->all()->wait\n"; | |
return 1; | |
}, | |
sub { | |
print gmtime . " subprocess done\n"; | |
} | |
); | |
$subprocess->ioloop->start unless $subprocess->ioloop->is_running; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment