I want to get an idea of what I do wrong

The problem with your first piece of code boils down to sub {}->(). This creates an anonymous sub and then immediately calls it, and ->run_p() gets the return value of this sub as its argument instead of the sub itself, as it should.

This shows how important SSCCE along with the Basic debugging checklist are, especially boiling down the code and adding more print statements. If you inspect the output of the following code, you'll see how the subs are being called immediately in the same process as the IO loop.

use Mojo::Base -strict, -signatures; use Mojo::IOLoop; Mojo::IOLoop->next_tick(sub ($loop) { say time-$^T, "s Loop PID is $$"; for my $i (0..1) { say time-$^T, "s Start of loop $i"; $loop->subprocess->run_p( sub ($j) { say time-$^T, "s Hello, from loop $i (arg $j) in PID $$!"; sleep 2; say time-$^T, "s Good bye, from PID $$!"; return "Return value of $i"; }->($i) )->then(sub ($result) { say time-$^T, "s Result from loop $i: $result"; })->catch(sub ($err) { say time-$^T, "s Subprocess error: $err"; }); say time-$^T, "s End of loop $i"; } }); Mojo::IOLoop->start;

Output:

0s Loop PID is 8353 0s Start of loop 0 0s Hello, from loop 0 (arg 0) in PID 8353! 2s Good bye, from PID 8353! 2s End of loop 0 2s Start of loop 1 2s Hello, from loop 1 (arg 1) in PID 8353! 4s Good bye, from PID 8353! 4s End of loop 1 4s Subprocess error: Can't locate object method "Return value of 0" vi +a package "Mojo::IOLoop::Subprocess" at /opt/perl5.28/lib/site_perl/5 +.28.1/Mojo/IOLoop/Subprocess.pm line 53. 4s Subprocess error: Can't locate object method "Return value of 1" vi +a package "Mojo::IOLoop::Subprocess" at /opt/perl5.28/lib/site_perl/5 +.28.1/Mojo/IOLoop/Subprocess.pm line 53.
and how to make it work, please!

Some frameworks allow passing arguments to functions like this, but I don't see anything in the Mojo::IOLoop::Subprocess documentation to indicate that Mojo does. So the answer is as you have already discovered: closures. If you have a complex function that you want to call with different arguments in the subprocess, then simply write it as a separate sub and call it from the closure (e.g. ->run_p(sub { my_complex_func($arg1, $arg2) })).

I already laid out the above code as a closure, so all you need to do for it to work is simply remove the ->($i) in the above, and the output is what you probably expect:

0s Loop PID is 8462 0s Start of loop 0 0s End of loop 0 0s Start of loop 1 0s End of loop 1 0s Hello, from loop 0 (arg Mojo::IOLoop::Subprocess=HASH(0x55ba4daf953 +0)) in PID 8463! 0s Hello, from loop 1 (arg Mojo::IOLoop::Subprocess=HASH(0x55ba4eebd13 +8)) in PID 8464! 2s Good bye, from PID 8463! 2s Good bye, from PID 8464! 2s Result from loop 0: Return value of 0 2s Result from loop 1: Return value of 1

In reply to Re: use Mojo::IOLoop::Subprocess - cannot pass argument to the subprocess by haukex
in thread use Mojo::IOLoop::Subprocess - cannot pass argument to the subprocess by PerlDiver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.