Hello:

Can anyone explain to me why this is a bad idea, or a better way of going 
about this?

My problem: I have to call 10 REST services sequentially before rendering a 
page. These calls in total take between 10 seconds to over a minute to 
return. (Corporate IT "Stuff". I don't control the REST services I'm 
calling). Using callbacks gets messy real quick for our problem. I've 
played with Mojo::IOLoop::Delay, but it also seems less then ideal for our 
problem. 

So I've been looking at how to wrap the user agent in a away that blocks my 
response but lets the event loop run. See below. I started with AnyEvent 
conditional variables, but was able to recreate the same idea without it.

It has the intended effect. I can make several long running web calls in a 
row that would normally block the entire event loop, and morbo will keep 
responding to other requests.

Input appreciated.

Thanks,

-Tripp

sub get2 {
my $self = shift;
my @args = @_;
 my $done = 0;
my @results;
 my $ua = Mojo::UserAgent->new(); 
 $ua->get(@args => sub {
$done = 1;
push(@results, @_);
});
 Mojo::IOLoop->singleton->one_tick until $done;  # Let the IOLoop run other 
things until our web call is done
 return @results; # return the results
}

-- 
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