use strict;
use warnings;
use Time::HiRes 'sleep';
use DBD::Pg ':async';
my $dbh = DBI->connect('dbi:Pg:dbname=postgres', 'postgres', '', {AutoCommit=>0,RaiseError=>1});
##
my $sth = $dbh->prepare("SELECT pg_sleep(?)", {pg_async => PG_ASYNC});
$sth->execute(5);
## , -
print "Your query is processing. Thanks for waiting\n";
check_on_the_kids();
while (!$dbh->pg_ready) {
check_on_the_kids();
## -
sleep 0.1;
}
print "The query has finished. Gathering results\n";
my $result = $sth->pg_result;
print "Result: $result\n";
my $info = $sth->fetchall_arrayref();
while () { my $ foo = compute_foo (); # heavy function # block until the previous request is executed $ dbh-> do ('UPDATE stats SET foo =?', {pg_async => PG_ASYNC + PG_OLDQUERY_WAIT}, $ foo); }
$ first_dbh-> do ('DELETE FROM old_data', {pg_async => PG_ASYNC}); $ second_dbh-> do ('UPDATE new_data SET status = 0', {pg_async => PG_ASYNC})
Source: https://habr.com/ru/post/67390/