Forum Programmation.perl thread et mysql

Posté par  .
Étiquettes : aucune
0
30
mai
2008
J'ai un petit code qui sur le papier devrait bien marcher mais seulement quand je le lance ben ça marche nettement moins bien


use threads;
use strict;
use DBI;
use warnings;
use diagnostics;

my @pth;
my $ptiming;
my $rep;
my $database = "...";
my $hostname = "localhost";
my $login = "root";
my $mdp = "";
my $dsn = "DBI:mysql:$database:$hostname";
my $sth;
my $dbh;

threads->new(\&main)->join;

sub main
{
while(1)
{
$dbh = DBI->connect($dsn,$login,$mdp) or die "ECHEC connexion";
@pth = ();
for(my $i=0;$i
{
$pth[$i] = threads->new(\&subroutine);
}
$ptiming = threads->new(\&timing);
$ptiming->join;
for(my $j=0;$j
{
$rep = $pth[$j]->join;
$sth = $dbh->prepare("INSERT INTO test VALUES('test')");
$sth->execute();
}
$dbh->disconnect
}
}

sub subroutine
{
return "oO";
}

sub timing
{
sleep(0);
}

Donc le premier tour de boucle se passe bien mais après, un warning sur le premier insert du 2eme tour et sur le 2 insert perl plante :

Use of uninitialized value in null operation during global destruction (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.

To help you figure out what was undefined, perl tells you what operation
you used the undefined value in. Note, however, that perl optimizes your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.

Use of uninitialized value in null operation during global destruction (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.

To help you figure out what was undefined, perl tells you what operation
you used the undefined value in. Note, however, that perl optimizes your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.

Argument "NAME" isn't numeric in null operation during global destruction (#2)
(W numeric) The indicated string was fed as an argument to an operator
that expected a numeric value instead. If you're fortunate the message
will identify which operator was so unfortunate.

Ca c'est tout le blabla auquel j'ai droit mais qui ne m'avance pas plus que ça ...Si quelqu'un avait une idée !?
  • # les variables globales c'est mal

    Posté par  (site web personnel) . Évalué à 3.

    tu fais du multi-threading soit ... mais ce n'est pas une raison pour ne pas limiter la porter de tes variables.

    - pourquoi toutes tes variables sont définies au debut et de manière globale ?
    - as tu la certitude que ton $dbh est bien initialisé ?
    - pourquoi te connectes tu puis te deconnectes tu as chaques fois dans ton while(1) ?
    - si tu maintiens ta connexion/deconnexion à chaque fois, pense à bien détruire ton objet de connexion ...
    - pourquoi tu n'essaierais pas de faire fonctionner ton script d'abord sans tous tes threads digne d'un java-junkie ?

Suivre le flux des commentaires

Note : les commentaires appartiennent à celles et ceux qui les ont postés. Nous n’en sommes pas responsables.