Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I love Parallel::ForkManager and I love DBD::mysql but they don't always get along so well. If you have DBI connections open in the parent process you'll get errors when the child processes exit. There's a few ways to deal with the problem, but here's the easiest. Just disconnect all handles in the parent and reconnect later after your parallel work is done:

# disconnect all database handles my %drivers = DBI->installed_drivers(); my @all_dbh = grep { defined } map { @{$_->{ChildHandles}} } value +s %drivers; $_->disconnect for @all_dbh; # now use Parallel::ForkManager as usual foreach my $job (@work) { $pm->start and next; # do the fork # connect in the child $dbh = DBH->connect(...); $pm->finish; # do the exit in the child process } $pm->wait_all_children; # now safe to reconnect in the parent $dbh = DBI->new(...);

The alternative is to keep the parent handles connected but set InactiveDestroy on then in the children. I don't prefer this because you also have to be sure the children won't accidently use the parent handles or explicitely disconnect() them. This may seem easy for simple scripts but code using ORMs like Class::DBI or Rose::DB::Object can hide DBI connections deep in their bowels. You might worry about the slowdown from all this disconnecting and reconnecting but I've yet to see it show up on a profiling run.

So there you have it. I wrote this note mostly for my own future reference since I've had to rediscover this a couple times now. Maybe it will help you too!

-sam

PS: This probably works for any DBI driver that uses sockets to talk to the database, but I can't say for sure.


In reply to Parallel::ForkManager and DBD::mysql, the easy way by samtregar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-20 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found