Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Forking Question

by RiotTown (Scribe)
on Jun 14, 2001 at 00:52 UTC ( [id://88206]=perlquestion: print w/replies, xml ) Need Help??

RiotTown has asked for the wisdom of the Perl Monks concerning the following question:

Yet another fork/parent-child relationship question. I've read the IPC docs and search perlmonks for answers, but I'm coming up a little short. My dilemma: I've got 4 databases to search and I'd like to do it concurrently. I've build a script that searches the 4 dbs using flat files and processes in the background, but it is more than a little messy. I'd like to have a process spawn 4 children, have them perform the necessary db connects/queries/disconnects, and then return the results (array of hash refs?) to the parent to be sorted. I found the following code posted by mikfire (here) and modified it some; I've gotten it to perform the connections and grab the results, but I'm having problems retaining the data in any type of structure once the child dies (I can print it out immediately, tho). Ideally I'd like to combine all of the arrays and sort them based on the hash keys. Is this possible?

Replies are listed 'Best First'.
Re: Forking Question
by Eradicatore (Monk) on Jun 14, 2001 at 01:15 UTC
    I've been doing some work with forking lately (see my posts if you want to know about perl/tk and forking and win32), and my initial thought after reading your question was to just open up pipes both ways (child to parent and parent to child) and then you can just have the child print a line back to the parent when it is done. Just format the line so it is easily split apart into the various parts of say a hash or array, etc.

    For example you could print this back:

    key1:element1,key2:element2 (and so on)

    Then the only thing you have to figure out (from the IPC docs or wherever) is how to wait for this in a nice way in the parent (unless you don't mind polling...). The parent just has to split the line, and put it back together.

    Admittedly, this is a hack. But sometimes its more fun to just get it working and move on to other cool parts of the program. ;)

    Justin Eltoft

    "If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

Re: Forking Question
by mikfire (Deacon) on Jun 14, 2001 at 09:22 UTC
    You may also want to check out IPC::Shareable or IPC::ShareLite if your system supports shared memory. I cannot claim to be conversant in either module, but it should be a fair fit for what you are trying to do. It would also eliminate the need to use pipes.

    If you decide not to do this, I am uncertain if you really need two way pipes. It seems the communication needs to flow only from child to parent. You can do this with

    open KID, "-|";
    which will fork a child process off and connect the child's STDOUT to the named file handles.

    mikfire

Re: Forking Question
by perigeeV (Hermit) on Jun 14, 2001 at 02:10 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2024-04-19 16:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found