http://www.perlmonks.org?node_id=38005

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

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.

Originally posted as a Categorized Question.

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

Replies are listed 'Best First'.
Re: How can I run a portion of code (like fork() does) w/o creating its own process?
by AgentM (Curate) on Oct 24, 2000 at 06:30 UTC
    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.
Re: How can I run a portion of code (like fork() does) w/o creating its own process?
by merlyn (Sage) on Oct 23, 2000 at 23:57 UTC
    You should prefork then, like Apache does.
Re: How can I run a portion of code (like fork() does) w/o creating its own process?
by nerdie (Initiate) on Jun 14, 2002 at 04:30 UTC
    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.
Re: How can I run a portion of code (like fork() does) w/o creating its own process?
by entropy (Sexton) on Jun 09, 2001 at 01:53 UTC
    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.