Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: use Mojo::IOLoop::Subprocess - cannot pass argument to the subprocess

by haukex (Archbishop)
on Mar 29, 2023 at 10:15 UTC ( #11151319=note: print w/replies, xml ) Need Help??


in reply to use Mojo::IOLoop::Subprocess - cannot pass argument to the subprocess

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

Replies are listed 'Best First'.
Re^2: use Mojo::IOLoop::Subprocess - cannot pass argument to the subprocess
by PerlDiver (Initiate) on Apr 03, 2023 at 18:43 UTC
    Thank you very much for your help! indeed, it works now!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11151319]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2023-09-21 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?