Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I am struggling to figure out how to use threads::shared variables, in this particular case Hash, amongst multiple threads. To make things as simple as I can right now I am not trying to run in threads but just code what I hope is the simplest of verifications. In this particular case I get a SQLite reference back from a connect but cannot figure out the correct reference/dereference syntax. In this particular example in the 3rd section of the code the SQLite prepare fails because the prior connect reference isn't the same, in my large use this would have a shared Hash of SQLite 'open' DB's (the connect references) which could be cycled through doing queries.
From the documentation on threads::shared I am 'sure' my lack of understanding is related to the comment "Shared variables can only store scalars, refs of shared variables, or refs of shared data (discussed in next section):" but I simply do not understand enough to figure out what to do.
Any help is appreciated.
Doug
the code example;
[root@itx2 ~]# cat x7.pl use DBI; use threads; use threads::shared; my $driver = 'SQLite'; my $database = ''; my $dsn = ''; my $userid = ''; my $password = ''; my $dbh; my $dsn; my $stmt; my $sth; my $rv; my @row; my %hash; my %hsh :shared; my $int :shared; my $String; my $Sdatabase; $Sdatabase = $ARGV[0]; $dsn = "DBI:$driver:dbname=$Sdatabase"; $dbh = DBI->connect($dsn, $userid, $password, { PrintError => 1, Ra +iseError => 1, AutoCommit => 0, ReadOnly => 1 }); $stmt = qq(select count ( * ) from asnipv4 ); $sth = $dbh->prepare( $stmt ); $rv = $sth->execute(); while( @row = $sth->fetchrow_array()) { $Iknt++; $String = join ',', @row; print "$Iknt -> $String \n"; } $dbh->disconnect(); print 'one' . "\n"; $dbh = DBI->connect($dsn, $userid, $password, { PrintError => 1, Ra +iseError => 1, AutoCommit => 0, ReadOnly => 1 }); $hash{$Sdatabase} = $dbh; $stmt = qq(select count ( * ) from asnipv4 ); $sth = $hash{$Sdatabase}->prepare( $stmt ); $rv = $sth->execute(); while( @row = $sth->fetchrow_array()) { $Iknt++; $String = join ',', @row; print "$Iknt -> $String \n"; } $hash{$Sdatabase}->disconnect(); delete $hash{$Sdatabase}; print 'two' . "\n"; $dbh = DBI->connect($dsn, $userid, $password, { PrintError => 1, Ra +iseError => 1, AutoCommit => 0, ReadOnly => 1 }); $hsh{"$Sdatabase"} = $dbh; $stmt = qq(select count ( * ) from asnipv4 ); $sth = $hsh{"$Sdatabase"}->prepare( $stmt ); $rv = $sth->execute(); while( @row = $sth->fetchrow_array()) { $Iknt++; $String = join ',', @row; print "$Iknt -> $String \n"; } $hsh{$Sdatabase}->disconnect(); delete $hsh{$Sdatabase}; print 'tre' . "\n"; exit; [root@itx2 ~]#
the execution failure;
[root@itx2 ~]# perl x7.pl /opt/asd/sql/dbi-indexed-GeoLite2.db-sqlite 1 -> 512468 one 2 -> 512468 two Invalid value for shared scalar at x7.pl line 60. [root@itx2 ~]#
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: threads::shared referencing confusion
by ikegami (Patriarch) on Aug 20, 2024 at 19:19 UTC | |
by Anonymous Monk on Aug 21, 2024 at 17:59 UTC | |
by Marshall (Canon) on Aug 22, 2024 at 07:26 UTC | |
by ikegami (Patriarch) on Aug 22, 2024 at 04:51 UTC | |
by Marshall (Canon) on Aug 21, 2024 at 21:03 UTC | |
by ikegami (Patriarch) on Aug 22, 2024 at 04:49 UTC | |
by etj (Priest) on Aug 22, 2024 at 14:36 UTC | |
| |
by Anonymous Monk on Aug 21, 2024 at 21:15 UTC | |
by GrandFather (Saint) on Aug 21, 2024 at 21:24 UTC | |
| |
by etj (Priest) on Aug 21, 2024 at 20:00 UTC |