in reply to Can't get multiple Parallel::ForkManager threads connecting to DBD::Pg database
Just an idea. Or a study or a sketch. As you like:
#!/usr/bin/env perl use strict; use warnings; use threads; use MCE::Hobo; use MCE::Shared; use DBI; use Data::Dump; use feature qw(say); use constant AMOUNT => 1; use constant MAX => 100; use constant DB => qq(dbi:SQLite:testomato.db); say qq($0 $$); sleep AMOUNT; my $shared = MCE::Shared->array(); my $cores = MCE::Util::get_ncpu(); MCE::Hobo->init( max_workers => $cores ); for my $id ( 1 .. MAX ) { my $hobo = MCE::Hobo->create( \&task ); say qq( $id ) . $hobo -> pid(); } MCE::Hobo->wait_all(); my $result = $shared->export; dd $result; sub task { my $db = DBI->connect( DB, "", "") or die DBI->errstr; my $result = $db->selectall_arrayref("SELECT * FROM fubar"); $db -> disconnect; $shared -> push($result); sleep AMOUNT; } __END__
The flow can be nicely observed in the process table.
Here is how to generate the example db:
Karls-Mac-mini:db karl$ sqlite3 testomato.db "create table if not exis +ts fubar(id INTEGER PRIMARY KEY, name TEXT, surname TEXT);" Karls-Mac-mini:db karl$ sqlite3 testomato.db "insert into fubar (name, + surname) values ('karl','goethebier');" Karls-Mac-mini:db karl$ sqlite3 testomato.db "select * from fubar"; 1|karl|goethebier Karls-Mac-mini:db karl$
As i don't have Postgres on my box and i don't want it i used SQLite to show the basic idea. A database is a database. I don't know if this example is helpful. But i guess at least it might be interesting. Best regards.
«The Crux of the Biscuit is the Apostrophe»
|
|---|