Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As I mention in my comment, Mojo::IOLoop doesn't concern with parallelism but with event loops. This means that operations which may take a long time may be started and then continued only once complete without blocking the other processes. In your example both the call to Mongo and the download are these types of long-running processes which one may want to put in the event loop. Mojolicious::UserAgent is non-blocking when used with a callback (the action called once the operation is complete). To do the same with Mongo you will need to be sure your Mongo library can be used in a non-blocking fashion.</p> <p>I have mocked up a little example which ignores the Mongo part (exercise for the reader) and uses the UserAgent in a non-blocking fashion.</p> <pre><code>#!/usr/bin/env perl use Mojolicious::Lite; helper download =&gt; sub { my ($c, $url, $target) = @_; my $ua = $c-&gt;app-&gt;ua-&gt;max_redirects(5); say "Starting new download ($url) ..."; $ua-&gt;get( $url, sub { my ($ua, $tx) = @_; say "Finished download ($url) ..."; $tx-&gt;res-&gt;content-&gt;asset-&gt;move_to($target); say "File saved ($url --&gt; $target)"; $c-&gt;render( text =&gt; "File saved ($url --&gt; $target)" ); }); }; any [qw/ get post /] =&gt; '/:action/:objid' =&gt; sub { my $self = shift; $self-&gt;render_later; # prevent auto-render #Parsing objectid my $objectid = $self-&gt;param('objid'); #verify url here ... # example url my $url = 'http://www.google.com'; $self-&gt;download($url,"final_name.xml"); }; app-&gt;start; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload