Update: I changed my mind on this - see my newest node at the end of the discussion.

Hey stefp,

Thanks for the example. I'm afraid I don't agree that the parent/child relationship can't be changed. I think in your shoes I would still be thinking about handling this with a pre-forked process. Or I would be trying to avoid the complexity of the parent/child as it is not clear to me the value that it provides.

I am probably going to butt out now, because without a better idea of why you cannot or are not willing to change the archetecture I can't be of much help. Would I be correct in thinking that this fork only happens once at the beginning of the process? I'm not saying you are wrong and you very well may be doing this the best way possible and if so I hope you can easily resolve your issues with the help of the monastery.

But before I butt out, let me ramble aimlessly for a bit. {g} If you must fork there are a few reasons why pre-forking a child strikes me as a good idea. First is it reduces process management issues. In your example you have no wait or waitpid which will eventually lead this process to fail if it is in a loop. With a single pre-forked child you can simple ask the child to exit and wait once at the end. It also makes signal handling less difficult (or perhaps just less critical).

If you are going to pre-fork, that child could just listen for a message from the parent saying "Get me this $file", and then the child reacts to that an passes the information pback over the socket pair and listen again for the next directive. It's different than what you are doing in your example, but depending upon what you are trying to accomplish it may be a cleaner way to do it.

Second, inheriting things like the Tk objects is obviously avoided if you pre-fork. This provides a savings in memory and in start-up time, particularly if the forking happens repeatedly. In an interactive program this may not be much of an issue since people are used to waiting for the hourglass so to speak.

Next, if the child isn't doing some tremendous value add stuff, forking at all doesn't seem to be helpful in this case (in fact it adds much complexity). The reason I see to fork is to better handle blocking issues but with a filehandle there are any number of ways in this issue in this. Using IO::Select on your filehandle in a loop lets you do processing and prevent blocking during your read process.

Well, anyway I wish you luck. If I think of anything helpful to add (as opposed to my babbling just now. :) ) I'll check back in.
{NULE}
--
http://www.nule.org


In reply to Re: Re: forking in Tk and letting the dad alone deal with X stuff by {NULE}
in thread forking in Tk and letting the dad alone deal with X stuff by stefp

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.