Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Can I use Mojo::IOLoop::Subprocess for a non-blocking process?

by talexb (Canon)
on Mar 23, 2019 at 04:05 UTC ( #1231586=perlquestion: print w/replies, xml ) Need Help??
talexb has asked for the wisdom of the Perl Monks concerning the following question:

I have a Mojolicious monitoring web app that's working well. I'd like to extend it to give it the ability to start scripts running.

My code is something like this:

use Mojolicious::Lite; use Mojo::IOLoop::Subprocess; ... post '/' => sub { ... my $message = 'INFO: Something useful'; my $cmd = 'cd /home/foo/bar && ./some-script.pl >some.out 2>some.e +rr'; my $subprocess = Mojo::IOLoop::Subprocess->new; $subprocess->run( sub { my $subprocess = shift; my $result = qx/$cmd/; return $result; }, sub { my ($subprocess, $err, @results) = @_; if ( $err ) { $message .= "ERROR: $err"; } else { $message = $results[0]; } } ); $subprocess->ioloop->start unless $subprocess->ioloop->is_running; ... $c->render(template => 'index'); };
In the $cmd, I'm cd'ing into a directory and running a known script. The good news is that this works .. the script does actually run fine, and the output and error files are created correctly. What I'm not expecting is that the Mojolicious web app doesn't return immediately while the script runs; rather, it waits for the script to finish.

The behaviour that I'd like is to see that the script started, with the web page refreshing a second or two later. At some point a couple of minutes later, the script will finish .. but I really don't care about the output.

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re: Can I use Mojo::IOLoop::Subprocess for a non-blocking process?
by Anonymous Monk on Mar 23, 2019 at 05:42 UTC
Re: Can I use Mojo::IOLoop::Subprocess for a non-blocking process?
by localshop (Monk) on Mar 26, 2019 at 15:08 UTC
    For this specific use case have you considered something like the following?
    system('my_command_which_will_not_block &');

      Yup .. that's what I ended up doing.

      my $cmd = "cd /home/foo/$row->{'dir'} && " . "./$row->{'name'}.pl >some.out 2>some.err &"; my $result = system ( $cmd );

      And it works fine. Thanks for following up.

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

        my $cmd = "cd /home/foo/$row->{'dir'} && " . "./$row->{'name'}.pl >some.out 2>some.err &"; my $result = system ( $cmd );

        Potential shell injection vulnerability ($row->{'dir'} and $row->{'name'}). Consider using fork and exec manually.

        Rough sketch:

        1. my $pid=fork() // die
        2. In the main program ($pid != 0), call waitpid($pid) at some point to wait for the end of the program you have forked before. Perhaps in a SiGCHLD handler.
        3. In the child process ($pid == 0), do the following:
        4. chdir("/home/foo/$row->{'dir'}") or die
        5. open STDOUT,'>','some.out' or die
        6. open STDERR,'>','some.err' or die
        7. exec("$row->{'name'}.pl") or die

        Related: The problem of "the" default shell

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231586]
Approved by kcott
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2019-04-18 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (105 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!