From: Ken Bloom Date: 2008-03-28T08:39:54+09:00 Subject: Re: fork On Wed, 26 Mar 2008 19:09:10 -0500, Joey Marino wrote: >> > I want to fork a child process and use instantiated objects from the >> parent >> > process. Is this possible? >> Yes, but it is a _copy_ of the original, so modifications in the child >> don't affect the parent. > [Note: parts of this message were removed to make it a legal post.] > > I see, > I have a class to handle interfacing to the database. The initialization > of the class will connect to the db and another method will close the > connection and there are query functions for handling the queries and > results. This object is then passed into new objects on their > initialization. The problem comes in when I iterate through a set of > objects creating a fork for each one. After the first object I am > getting a "database went away error". My code looks like the following: When you fork() a new process, both the memory is copied, but both the parent and child processes share the same open files, including the same socket (because there's no real concept of duplicating the socket at both ends). When one process quits, it closes the connection automatically (this is done by a finalizer on the database handle, or something like that, possibly buried in the C library). In the MySQL protocol[1], this sends a command over the socket to the MySQL server, and the server and client close their connection. The second process, which still has the socket open, now discovers that the connection is closed and reports that the server has gone away. A good solution to this problem is to fork a DRb server that serves up the database access stuff, and allocate the Mysql connection only inside that process. DRb connections will work just fine across forks. (I know I implemented this before, but I can't find the sample code.) --Ken [1] http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol -- Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/