Question regarding Perl, DBI, and fork()

Started by Allen Landsidelover 22 years ago3 messagesgeneral
Jump to latest
#1Allen Landsidel
all@biosys.net

I have a job-processing backend written in perl, talking to a (of course)
postgres database. The perl app has a master process that checks the
database periodically for jobs that need processed. When there are
available jobs, it grabs some number of them (up a few hundred), and
fork()'s to create job processing processes, one per job, again up to a few
hundred.

I know the child processes inherit the db handle, as I learned the hard way
about InactiveDestroy ;) so my question is this:

Can the children (safely) use this handle to run database queries, or
should I just consider the handle garbage? I didn't want to "just try and
see" because there's always the possibility it could work "by accident"
during the low-load testing, and fail at some other time.

Relevant versions of everything are:

FreeBSD 4.8 (Tracking RELENG_4, now 4.9-RC)
PostGreSQL 7.3.4
perl 5.003 (FreeBSD default)
perl DBI 1.37
perl DBD::Pg 1.22

I'm on the list, CC directly only if you're bored. Thanks.

-Allen

#2Martijn van Oosterhout
kleptog@svana.org
In reply to: Allen Landsidel (#1)
Re: Question regarding Perl, DBI, and fork()

On Sun, Oct 26, 2003 at 03:04:29PM -0500, Allen Landsidel wrote:

Can the children (safely) use this handle to run database queries, or
should I just consider the handle garbage? I didn't want to "just try and
see" because there's always the possibility it could work "by accident"
during the low-load testing, and fail at some other time.

No, you can't. Even multithread programs can't use the same connection at
the same time (they can if serialised or if they use different connections).

Now, getting rid of the handle without it closing the connection would be a
trick.
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

"All that is needed for the forces of evil to triumph is for enough good
men to do nothing." - Edmond Burke
"The penalty good people pay for not being interested in politics is to be
governed by people worse than themselves." - Plato

#3Robert Creager
Robert_Creager@LogicalChaos.org
In reply to: Martijn van Oosterhout (#2)
Re: Question regarding Perl, DBI, and fork()

When grilled further on (Mon, 27 Oct 2003 09:40:40 +1100),
Martijn van Oosterhout <kleptog@svana.org> confessed:

On Sun, Oct 26, 2003 at 03:04:29PM -0500, Allen Landsidel wrote:

Can the children (safely) use this handle to run database queries, or
should I just consider the handle garbage? I didn't want to "just try and
see" because there's always the possibility it could work "by accident"
during the low-load testing, and fail at some other time.

No, you can't. Even multithread programs can't use the same connection at
the same time (they can if serialised or if they use different connections).

Now, getting rid of the handle without it closing the connection would be a
trick.

I've had good success setting InactiveDestroy (as Allen found), and in my
children, I re-connect to the db. Works for me, although I'm only using up to
16 children + parent.

Cheers,
Rob

--
17:35:03 up 86 days, 10:58, 4 users, load average: 2.09, 2.04, 2.00