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

El martes, 24 de marzo de 2015, 1:34:11 (UTC-3), Alexey Stavrov escribió:
>
> 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.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to