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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |