Yes, as documented at https://metacpan.org/pod/Mojo::IOLoop::Delay#wait,
the ->wait method does nothing if the event loop is currently running. You
do not want it to do anything in this case, it is a bad idea to run the
event loop during its own event callback. So it is by design that any
non-blocking functionality you queue up for the event loop will not occur
until the current callback returns. You likely want to use the delay helper
(https://metacpan.org/pod/Mojolicious::Plugin::DefaultHelpers#delay)
instead of straight Mojo::IOLoop->delay to set this up correctly within a
Mojolicious controller action.
https://metacpan.org/pod/Mojolicious::Guides::Cookbook#REAL-TIME-WEB has
some more examples of this style of response.

On Wed, Mar 29, 2017 at 12:25 AM, Jeremy Begg <[email protected]>
wrote:

> Hi Dan,
>
> Thanks for the tip about the user agent object.  But something's still not
> right.
>
> The following code works fine when run as a standalone Perl program.
>
> use feature 'say';
>
> use Mojo::IOLoop;
> use Mojo::IOLoop::Delay;
> use Mojo::UserAgent;
>
> my $ua = Mojo::UserAgent->new;
>
> my %dblist = (solarmon => 1,
>                 solarmon_5min => 1,
>                 solarmon_30min => 1,
>                 solarmon_day => 1
>                 );
> my $url = 'http://wasd.vsm.com.au/echo/';
> my $query = 'justanexample';
> say "Initiating web requests ...";
> Mojo::IOLoop->delay(
>   sub {
>     my $delay = shift;
>     foreach my $db (keys %dblist) {
>         say "   Requesting $db ...";
>         $ua->get("$url$db&q=$query" => $delay->begin);
>     }
>   },
>   sub {
>     my $delay = shift;
>     say "    Processing ", scalar @_, " results ...";
>     foreach my $tx (@_) {
>         say "        Got ", $tx->result->body;
>     }
>   }
> )->wait;
> say "IOLoop->delay() test done";
>
> and displays:
>
> Initiating web requests ...
>    Requesting solarmon_day ...
>    Requesting solarmon ...
>    Requesting solarmon_30min ...
>    Requesting solarmon_5min ...
>     Processing 4 results ...
>         Got GET /echo/solarmon_day&q=justanexample HTTP/1.1
> content-length: 0
> accept-encoding: gzip
> host: wasd.vsm.com.au
> user-agent: Mojolicious (Perl)
>
>
>         Got GET /echo/solarmon&q=justanexample HTTP/1.1
> user-agent: Mojolicious (Perl)
> host: wasd.vsm.com.au
> accept-encoding: gzip
> content-length: 0
>
>
>         Got GET /echo/solarmon_30min&q=justanexample HTTP/1.1
> user-agent: Mojolicious (Perl)
> host: wasd.vsm.com.au
> accept-encoding: gzip
> content-length: 0
>
>
>         Got GET /echo/solarmon_5min&q=justanexample HTTP/1.1
> accept-encoding: gzip
> content-length: 0
> user-agent: Mojolicious (Perl)
> host: wasd.vsm.com.au
>
>
> IOLoop->delay() test done
>
> BUT if I embed that code into my Mojolicious web application (run using '
> morbo') it behaves as if the *$delay->wait* did nothing:
>
> Initiating web requests ...
>
> IOLoop->delay() test done
>
> Received 0 results
>
>     Requesting solarmon_day ...
>
>     Requesting solarmon_5min ...
>
>     Requesting solarmon_30min ...
>
>     Requesting solarmon ...
>
> The requests are (apparently) being issued AFTER the *$delay->wait*
> completes?
>
>
> --
> 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 https://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to