Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In General is an event loop only for IO work? and what exactly is an IO job?
For example let's say a request comes into mojolicious which is then making an outbound http request to an API to get some data while not blocking the user in the meantime.
Is that IO job and how would Mojo handle it?what if instead of the http request I want to asynchronously make a lengthy calculation and then return a value to the user? Is that handled by the event loop too?

Replies are listed 'Best First'.
Re: Mojo::IOLoop and asynchronous work
by Anonymous Monk on Sep 29, 2018 at 17:29 UTC
    Yes. Making an outbound HTTP request requires a little bit of CPU to set up and send the request and the rest of the time is just waiting for the server to respond, in other words waiting for IO, and that's what event loops do best. The cookbook has examples that might help. Now if you want to do lengthy CPU operations without blocking the process and the event loop, you can use subprocesses.
      There it says "You can also use subprocesses, created with "subprocess" in Mojo::IOLoop, to perform computationally expensive operations without blocking the event loop." and "Operation that would block the event loop for 5 seconds"
      what kind of operation would block the event loop? And staying non blocking isn't what the event the loop is for anyway?