Help with Mojo::IOLoop->delay()

377 views
Skip to first unread message

Chuck Finley

unread,
Jan 2, 2013, 11:33:50 PM1/2/13
to mojolicious
Happy New Year everyone!

I'm trying to do something based on this Mojolicious cookbook: https://metacpan.org/module/SRI/Mojolicious-3.71/lib/Mojolicious/Guides/Cookbook.pod#Backend-web-services, but in the code below I only get the results from the first 3 $ua->get() from the first sub{} in delay() and not the other 3 from the middle sub{} in delay(). If I read the docs correctly, the 3 sub{}'s inside of delay() are executed sequentially. What am I missing here? 

Thanks!

#!/usr/bin/env perl

use strict;
use warnings;

use feature 'say';

use Mojolicious::Lite;
use Mojo::UserAgent;
use Mojo::IOLoop;
use Mojo::URL;

get '/test' => sub {
   my $self = shift;

   my @data;

   my $url = Mojo::URL->new('http://search.twitter.com/search.json');
   my $ua  = Mojo::UserAgent->new;
   Mojo::IOLoop->delay(

      sub {
         my $delay = shift;
         $ua->get($url->clone->query({q => 'mojo'}) => $delay->begin);
         $ua->get($url->clone->query({q => 'django'}) => $delay->begin);
         $ua->get($url->clone->query({q => 'playframework'}) => $delay->begin);
      },

      sub {
         my ($delay, @res) = @_;
         foreach my $tx (@res) {
            if ($tx->success) {
               push @data, $tx->res->json('/results/0/text');
            }
         }

         $ua->get($url->clone->query({q => 'perl'}) => $delay->begin);
         $ua->get($url->clone->query({q => 'python'}) => $delay->begin);
         $ua->get($url->clone->query({q => 'scala'}) => $delay->begin);
      },

      sub {
         my ($delay, @res) = @_;
         foreach my $tx (@res) {
            if ($tx->success) {
               push @data, $tx->res->json('/results/0/text');
            }
         }
         $self->render(json => {data => \@data});
      }
   );

};

app->start;

Sebastian Riedel

unread,
Jan 3, 2013, 3:51:31 AM1/3/13
to mojol...@googlegroups.com
> What am I missing here?

$ua goes out of scope too early.

--
Sebastian Riedel
http://twitter.com/kraih
http://mojolicio.us

Chuck Finley

unread,
Jan 3, 2013, 11:20:31 AM1/3/13
to mojol...@googlegroups.com
Ah, so besides using the controller's ua ($self->ua->get()) how can I do the requests with a new ua without it going out of scope? I tried creating a new ua inside the sub{}s I make the requests but that didn't solve the problem.

Thanks! 


--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To post to this group, send email to mojol...@googlegroups.com.
To unsubscribe from this group, send email to mojolicious...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mojolicious?hl=en.


sri

unread,
Jan 3, 2013, 12:32:23 PM1/3/13
to Mojolicious
> how can I do the requests with a new ua without it going out of scope?

There are many ways, here are two.

helper my_ua => sub { state $ua = Mojo::UserAgent->new };

or

my $ua = $self->stash->{ua} = Mojo::UserAgent->new;

--
sebastian

Chuck Finley

unread,
Jan 3, 2013, 12:36:07 PM1/3/13
to mojol...@googlegroups.com

Awesome! Thank you very much!

amarnus

unread,
Jun 23, 2014, 6:22:00 AM6/23/14
to mojol...@googlegroups.com
People who just stumbled upon this thread might be interested to know that ua is available as a default helper since Mojolicious 4.13.

Reply all
Reply to author
Forward
0 new messages