You can also just fork + wait.

Note that if you're using a single connection to mysql, with threads our without them, you can't start multiple simultaneous commands through a single connection; db connections don't work that way (unless mysql has an extended sql command set to support background jobs, anyway; without getting stupid complicated I don't see one, though)! But you can open the connection in the subprocesses (easier to do with full processes, not threads), and run the command in each.

It doesn't seem like you're looking for data back from the subprocesses, which makes this pretty simple, for instance:

#!/usr/bin/perl use feature ':5.18'; my @commands = (sub {say "hi"},sub {say "there"},sub {sleep 2},sub {ex +ec("echo bye")}); my %jobs; for(@commands) { my $pid = fork(); die "bad fork" unless defined $pid; if($pid==0) { # child eval { $_->(); }; $@ and die "cmd failed: $@"; exit ($? ? $? : 0); } # parent if($pid<=0) { warn "job $_ failed ($pid): $!"; } else { $jobs{$pid}={cmd=>$_, status => "running"} } # here is where you can check for having too many jobs running if # you want to set a limit and wait for them. } # now wait for my jobs until I'm out of children. while(%jobs) { my $pid = wait(); last if($pid == -1); # no children = no more waiting next unless exists $jobs{$pid}; #if it's not a job we're watching, w +e don't care. #do any special handling, then... my $job = $jobs{$pid}; say "$pid ($job->{cmd}) finished with code $?"; delete $jobs{$pid}; } #continue on with the program. say "done";

In reply to Re: Multiple asynchronous execution of commands by mneme
in thread Multiple asynchronous execution of commands by ibm1620

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.