Skip to content

Instantly share code, notes, and snippets.

/test.pl Secret

Created June 7, 2015 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3d054eaa897c19350946 to your computer and use it in GitHub Desktop.
Save anonymous/3d054eaa897c19350946 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use 5.10.1;
use Data::Dumper;
use DBD::Pg ':async';
use DBI;
use IO::Poll qw(POLLIN POLLPRI);
# Fake event loop
sub is_readable { !!(IO::Poll::_poll(@_[0, 1], my $m = POLLIN | POLLPRI) > 0) }
my $dbh = DBI->connect('dbi:Pg:dbname=test', 'tester', 'testing');
open my $socket, '<&', $dbh->{pg_socket} or die "Can't dup: $!";
# First query
my $sth
= $dbh->prepare_cached('select pg_sleep(3)', {pg_async => PG_ASYNC}, 3);
$sth->execute();
# Fake waiting for first query to finish with an event loop
while (1) {
say 'Not readable yet' and next unless is_readable(1000, fileno $socket);
$sth->pg_ready ? last : say 'No result yet';
}
say 'First result: ', $sth->pg_result;
say Dumper $sth->fetchrow_hashref;
# Second query
$sth = $dbh->prepare_cached('select pg_sleep(3)', {pg_async => PG_ASYNC}, 3);
$sth->execute();
# Fake waiting for second query to finish with an event loop
while (1) {
say 'Not readable yet' and next unless is_readable(1000, fileno $socket);
$sth->pg_ready ? last : say 'No result yet';
}
say 'Second result: ', $sth->pg_result;
say Dumper $sth->fetchrow_hashref;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment