Skip to content

Instantly share code, notes, and snippets.

@awhatson
Created August 5, 2013 07:13
Show Gist options
  • Save awhatson/6154016 to your computer and use it in GitHub Desktop.
Save awhatson/6154016 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Modern::Perl;
use DBI;
use DBD::Pg qw(:async);
use Coro;
use AnyEvent;
use Time::HiRes qw(time);
sub query($) {
my $start = time;
my $sql = shift;
my $dbh = DBI->connect('dbi:Pg:host=10.0.1.120;dbname=postgres');
my $sth = $dbh->prepare($sql, { pg_async => PG_ASYNC });
$sth->execute;
until ($dbh->pg_ready) {
my $cb = rouse_cb;
my $readable = AE::io ($dbh->{pg_socket}, 0, $cb); # pg_socket is readable
my $timeout = AE::timer (1, 0, $cb); # or one second timeout
rouse_wait($cb);
}
$sth->pg_result;
printf "Query completed in %.5fs\n", time - $start;
}
my $start = time;
my @threads = (
async { query 'SELECT pg_sleep(1)' },
async { query 'SELECT pg_sleep(1)' },
async { query 'SELECT pg_sleep(1)' },
);
$_->join for @threads;
printf "Program completed in %.5fs\n", time - $start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment