Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Syntactic Confectionery Delight
 
PerlMonks  

How can I run a portion of code (like fork() does) w/o creating its own process?

( #38005=categorized question: print w/ replies, xml ) Need Help??
Contributed by david28 on Oct 23, 2000 at 23:51 UTC
Q&A  > programs and processes


Description:

I need to run a block of code, a subroutine, but creating a pid for each is overkill. Its likely to run at least 80 times a minute, for days on end.

Answer: How can I run a portion of code (like fork() does) w/o creating its own process?
contributed by AgentM

You might try experimenting with threads as they are "experimental". use Threads; but you'd better read this post by me. Modern OSs provide threads but Perl has a deficiency that basically kills any thread use.

Answer: How can I run a portion of code (like fork() does) w/o creating its own process?
contributed by Zombie nerdie

You might want to try redesigning your code to use POE (Perl Object Environment). This lets you write multitasking programs often without fork() overhead. You can design your 'child' subroutines as new Sessions, which is synonymous to a process, but without using the underlying fork() system call.

Answer: How can I run a portion of code (like fork() does) w/o creating its own process?
contributed by merlyn

You should prefork then, like Apache does.

Answer: How can I run a portion of code (like fork() does) w/o creating its own process?
contributed by entropy

If your 'child' subroutines don't need to run in the background, just do an eval{}. If you need the main code to run at the same time as the children, but two children won't run at once, then you can do one fork and re-use the child process for each task you have to do.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2014-04-21 11:22 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (493 votes), past polls