Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found