$slot = 1; my $pm = new Parallel::ForkManager( 2 ); foreach $key ( sort { lc $a cmp lc $b || $a cmp $b } keys %array ) { if( $slot == 2 ){ $slot = 1; } elsif( $slot == 1 ){ $slot = 2; } $pm->start and next; if( ! -e "/mnt/storage/videotemp/$key.hevc" ) { $cmd = 'WINEPREFIX=~/.wine64_'.$slot.' wine ... ; print "\n$cmd\n\n"; exec($cmd); } $pm->finish; } $pm->wait_all_children;