in reply to Advice: Async Options for fire-and-forget subroutine

What version of Perl are you running? There is an interesting new module called Future / Future::AsyncAwait. It requires Perl 5.24 atm though.

With it, what you want is as easy as:
async sub fire-and-forget { do-stuff(); } fire-and-forget(); #returns immedeatly, runs in parallel

The author gave a talk about this at this years Perl Conference in Amsterdam1.

1This is where Vince Vega had the time of his life.


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: Advice: Async Options for fire-and-forget subroutine
by mwb613 (Beadle) on Oct 21, 2017 at 06:45 UTC

    Since I am stuck with 5.16 I tried another module, Async. It seems to work ok but there's one bit of behavior that I didn't expect (though I probably should have): if the parent script finishes, the subroutine fired by Async finishes as well (I was testing some writes after a sleep command). I guess this makes sense since the process is dying and thus everything on it's stack gets popped but I didn't comprehend this when I first started thinking about the problem.

    I haven't yet tested it within the OpenSIPs module and don't know enough about how it works to wager a guess as to whether it keeps a persistent Perl env going while it operates (I know it spawns individual worker processes so each of them might have a unique env as well). Is it an unreasonable expectation to keep looking for a module that might be able to "fire-and-forget" a subroutine that can keep running even if the parent process dies?

      In most cases, a parent process (or thread) should wait for its children to finish. In the case of processes, this (usually) cleans up zombies. In the case of threads, you have to wait until the threads are finished (or some reasonable time out) before ending the main, as this will also end the process containing all the threads.

      It is possible for child processes to "detach" from their parent. I'm pretty sure that most of the fork manager type modules in CPAN will have an option for this.

      Paraphrasing a fictitious soldier, "Fire-and-forget is fine as long as you never forget."

Re^2: Advice: Async Options for fire-and-forget subroutine
by mwb613 (Beadle) on Oct 20, 2017 at 21:01 UTC

    Thanks!

    Unfortunately, I am stuck on 5.16 on RHEL

Re^2: Advice: Async Options for fire-and-forget subroutine
by Dave05 (Beadle) on Nov 12, 2019 at 22:13 UTC

    That doesn't work and I can't figure out from the docs how to get it to work.

    use Future::AsyncAwait; async sub _count { my $i; do { $i++ } until $i == 50_000_000; say "... counted"; } say "Before"; _count(); say "After"; Output is: Before ... counted After

    I tried this, which the buzzing in my head tells me should work, but no dice:

    use Future::AsyncAwait; async sub _add { my $one1 = await _count(); my $one2 = await _count(); return $one1 + $one2; } async sub _count { my $i; do { $i++ } until $i == 50_000_000; say "... counted"; return 1; } say "Before"; my $f = _add(); say "After"; say $f->get; Output: Before ... counted ... counted After 2