Problem with Mojo::Pg

72 views
Skip to first unread message

Alexey Stavrov

unread,
Mar 24, 2015, 12:34:11 AM3/24/15
to mojol...@googlegroups.com
I have this code which not output "666", "End Inserts", "End". What i do wrong?

#!/usr/bin/perl


use strict;
use warnings;
use v5.10;


use Mojo::Pg;
use Mojo::IOLoop;


my $pg = Mojo::Pg->new('postgresql://alex@/test');


my $db = $pg->db;


$db
->query(q{create table if not exists qwe (num integer, text text)});


sub upsert {
 
Mojo::IOLoop->delay(
   
sub {
     
my $delay = shift;
     
for my $i (1 .. 30) {
        warn
"update: $i";
        $db
->query(q{update qwe set num = ? where text = ?}, $i, "$i" => $delay->begin);
     
}
   
},
   
sub {
     
my $delay = shift;


      $delay
->pass;


     
for my $i (1 .. 30) {
       
my ($err, $res) = (shift, shift);
       
unless ($res->rows) {
         
my $end = $delay->begin;
          say
"insert $i";
          $db
->query(q{insert into qwe(num, text) values (?, ?)}, $i, "$i" => sub { say '666'; $end->(@_); });
       
}
     
}
   
},
   
sub {
      say
'End inserts';
     
Mojo::IOLoop->stop;
   
}
 
);
}


upsert
();


Mojo::IOLoop->start;


say
'End';



Thanks.

Daniel Mantovani

unread,
Mar 24, 2015, 9:57:49 AM3/24/15
to mojol...@googlegroups.com
In Mojo::IOLoop either $delay->begin or $delay->end must be invoked BEFORE you leave each step. It doesn't seem to be the case in the first step.

There is a very nice explanation of what is hapening here: http://www.slideshare.net/deepfountainconsulting/asynchronous-programming-patterns-in-perl. There is even a very similar case in the quiz # 1, page 91. Just writing $delay->begin inside a callback does not mean you are actually invoking it before you leave the step.

Second step looks fine, I tried but couldn't actually make it work :( but hope this comment will help.

BR,
Daniel

Dan Book

unread,
Mar 24, 2015, 10:01:18 AM3/24/15
to mojol...@googlegroups.com
That is not correct. The callback from $delay->begin is intended to be invoked when the non-blocking activity is ready, after the control has returned to the event loop.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Alexey Stavrov

unread,
Mar 24, 2015, 10:22:06 AM3/24/15
to mojol...@googlegroups.com
You are wrong. First callback is correct and second callback is called.

If "$db" replace on "$pg->db" then it works fine, but i don't know why this example not work.

вторник, 24 марта 2015 г., 18:57:49 UTC+5 пользователь Daniel Mantovani написал:

Dotan Dimet

unread,
Mar 24, 2015, 10:52:13 AM3/24/15
to mojol...@googlegroups.com
There's a $delay->pass at the 2nd line of the 2nd step, which I think will cause it to skip right down to the 3rd step?
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Alexey Stavrov

unread,
Mar 24, 2015, 1:29:09 PM3/24/15
to mojol...@googlegroups.com
Yes, you right. May be need to do truncate table qwe at least and delete "$delay->pass;" line to reduce number of lines in main logic.

вторник, 24 марта 2015 г., 19:52:13 UTC+5 пользователь Dotan Dimet написал:
Reply all
Reply to author
Forward
0 new messages