Le 06/01/2012 05:37, Chloe Sival a écrit :
> J'ai donc un script qui s'execute normalement, pour le moment, ma
> logique est du style :
>
> #!/usr/bin/perl
>
> &action1;
> &action2;
> &action3;
> &traitement
>
> Je voudrais "exécuter" simultanément les trois "&actionsX" et que
> &traitement s’exécute ensuite quand les trois actions auront fini
> leur jobs.
>
Un script simpliste :
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use Time::HiRes qw( time );
# définitions des actions
sub action1 {
print "Début thread 1\n";
sleep 10;
print "Fin thread 1\n";
}
sub action2 {
print "Début thread 2\n";
sleep 8;
print "Fin thread 2\n";
}
sub action3 {
print "Début thread 3\n";
sleep 12;
print "Fin thread 3\n";
}
my $start = time(); # top départ
# création et lancement des threads
my $thread1 = threads->create(\&action1);
my $thread2 = threads->create(\&action2);
my $thread3 = threads->create(\&action3);
# attente que chaque thread se termine (et nettoyage)
$thread1->join;
$thread2->join;
$thread3->join;
my $end = time(); # top d'arrivée
print "time =", $end-$start, "\n";
__END__
Sur ma machine, j'obtiens:
Début thread 1
Début thread 2
Début thread 3
Fin thread 2
Fin thread 1
Fin thread 3
time =12.0276210308075
On voit que le temps d'exécution du script est proche du temps
d'exécution du thread le plus long : c'est toujours comme ça quand on
fait roupiller les threads ;-)
Avec de vraies "actions", c'est beaucoup moins favorable.
Essayez avec vos routines actions et chronométrez pour voir si ça vaut
le coup d'utiliser des threads.
HTH
--
J-L
http://www.bribes.org/perl/