Portál AbcLinuxu, 21. července 2025 12:33
script1.pl | script2.pl
script2.pl sub update { my ($item) = @_; print "item to DB: $item\n"; sleep(2); if (my $dbh = DBI->connect('DBI:mysql:dataWeb', 'xxx', 'xxx')) { my $query="UPDATE onWeb SET name='".$item."' WHERE id=0;"; my $sth = $dbh->prepare($query);#zkousel jsem pouzit i $dbh->do ale je to stejny my $rv = $sth->execute(); $dbh->disconnect; } else { print "Could not connect to database: $DBI::errstr"; } } my $scThread=0; while (my $line = <STDIN>) { if ($line ne "") { chomp ($line); $scThread = threads->create(\&update, $line); } }Nevite nekdo prosim vas coje spatne? Kde mam udelat zmenu, aby script bezel jak ma? Soft mysql 5.0.51a a perl 5.10.
script2.pl sub update { my ($item,$dbh) = @_; print "item to DB: $item\n"; sleep(2); my $query="UPDATE onWeb SET name='".$item."' WHERE id=0;"; my $sth = $dbh->prepare($query);#zkousel jsem pouzit i $dbh->do ale je to stejny my $rv = $sth->execute(); } my $scThread=0; my $dbh = DBI->connect('DBI:mysql:dataWeb', 'xxx', 'xxx'); while (my $line = <STDIN>) { if ($line ne "") { chomp ($line); $scThread = threads->create(\&update, $line, $dbh); } } $dbh->disconnect;Hlaseni perlu:
Thread 2 tady dochazi ke zvysovani cisla vlakna terminated abnormally: DBD::mysql::db prepare failed: handle 2 is owned by thread 198e010 not current thread 1e2b7e0 (handles can't be shared between threads and your driver may need a CLONE method added)Pridal jsem teda shared:
pridano use threads::shared; ... my $scThread=0; my $dbh = DBI->connect('DBI:mysql:dataWeb', 'xxx', 'xxx'); share ($dbh); while (my $line = <STDIN>) { ...perl hlasi
Thread 1 terminated abnormally: dbih_getcom handle threads::shared::tie=SCALAR(0xe739c0) is not a DBI handle at script2.pl <STDIN> line 1
Thread 2 terminated abnormally: dbih_getcom handle threads::shared::tie=SCALAR(0xe739c0) is not a DBI handle at script2.pl <STDIN> line 2
ps -p 14814 uH
use threads; sub ThreadRoutine {} while (1) { threads->new(\&ThreadRoutine)->join; }
use threads; use threads::shared; my $line; my $thr_end; my @thr_data; my $thr_worker; share($thr_end); share(@thr_data); $thr_end = 0; $thr_worker = threads->create(\&thr_sub); @thr_data = (); sub thr_sub { my $dbh = DBI->connect('DBI:mysql:dataWeb;mysql_server_prepare=1', 'user', 'password'); my $qry = "UPDATE onWeb SET name=? WHERE id=0"; my $sth; my $tmp; die "Can't connect to database" if (!$dbh); $sth = $dbh->prepate($qry); while (!$thr_end) { { lock(@thr_data); $tmp = shift(@thr_data); } $sth->execute($tmp) if(defined($tmp)); } lock(@thr_data); $sth->execute($_) for (@thr_data); $dbh->disconnect(); } while ($line = <STDIN>) { chomp($line); next if ($line =~ /^\s*$/); { lock(@thr_data); push @thr_data, $line; } } $thr_end = 1; $thr_worker->join();
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.