Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Can't get multiple Parallel::ForkManager threads connecting to DBD::Pg database

by karlgoethebier (Abbot)
on Jul 24, 2021 at 11:26 UTC ( [id://11135358]=note: print w/replies, xml ) Need Help??


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»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11135358]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-24 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found