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

Hello Monks I have a perl program that does some job. The same program has to be put in scheduler with "n" instances to run all the instances at the same time. What I was thinking is to put it in a subroutine and call it that many number of times. But the problem is how to multi-task it i.e. fire all the instances at the same time? Please advise. Thanks

Replies are listed 'Best First'.
Re: Multitasking in Perl
by merlyn (Sage) on Feb 17, 2005 at 19:31 UTC
Re: Multitasking in Perl
by BrowserUk (Pope) on Feb 17, 2005 at 21:23 UTC
    i.e. fire all the instances at the same time?

    So long as you realise that unless you have 'n' processors, your 'n' instances will not al be running "at the same time".

    And, just so the "other option" doesn't go unmentioned. If you were thinking "put it in a subroutine and call it that many number of times" looked vaguely like this:

    sub doit { .... } doit( $args[ $_ ] ) for 0 .. $n-1;

    Then

    use threads; sub doit { ... } my @threads = map{ threads->new( \&doit, $args[ $_ ] ) } 0 .. $n - 1; $_->join for @threads;

    is about a simple as multi-tasking gets.


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
Re: Multitasking in Perl
by friedo (Prior) on Feb 17, 2005 at 19:41 UTC
    You could do it with a fork pretty easily.

    for(1..10) { my $pid = fork; unless($pid) { do_some_task; exit; } }
Re: Multitasking in Perl
by Tanktalus (Canon) on Feb 17, 2005 at 20:30 UTC

    As has already been stated, fork is the most likely solution (until perl6 when the threading support will likely finally be intrinsic to the language).

    However, please be aware that fork creates subprocesses. These subprocesses are independant of your parent process. Thus, merely setting a value of "$done = 1" in your subprocess will not affect the parent process in any way, shape, or form. Same in the reverse - the parent setting a variable won't affect the child (after the fork occurs). If this is acceptable (the subprocesses don't interact with each other nor with the parent process - e.g., they transform files on disk), then great. If not, then you'll need to also investigate methods of inter-process communication (IPC).

    Update: BrowserUk notes using threads. Threads have similar issues to the above issues with fork from what I understand. The method of inter-thread communication in perl is not quite as simple as it is in some other languages, such as Java. All methods available to fork are available, plus sharing variables directly. However, upon reading the threads and threads::shared documentation, I discarded it as too cumbersome (and volatile), although I desperately miss thread usage when programming in perl (compared to other languages).

Re: Multitasking in Perl
by FitTrend (Pilgrim) on Feb 18, 2005 at 02:10 UTC

    If you have to do this on windows, Parallel::ForkManager and threads can accomplish this.

Re: Multitasking in Perl
by spurperl (Priest) on Feb 18, 2005 at 10:15 UTC
    There is something I don't understand about your question. Do you need multitasking on the OS level, or on the Perl level ?

    If you have a program (be it an executable or a Perl script run by the Perl interpreter), you can run multiple instances of it on all the common OSes. Common OSes also have schedulers, "cron" on Unixes, and "at" (or "System tools" -> Scheduled tasks) on Windows.

    On the other hand, if you want to run multiple instances of some task from your Perl script, the other monks gave you good ideas - basically your options are processes or threads. What suits you most depends on the specifics of your application.

    I'm sure that if you post a clarification telling exactly what you want done, you will get very good answers.

Re: Multitasking in Perl
by perlfan (Vicar) on Feb 19, 2005 at 02:03 UTC
    How about:
    #!/bin/sh
    dosomejob.pl& 
    dosomejob.pl& 
    dosomejob.pl& 
    dosomejob.pl&
    
Re: Multitasking in Perl
by gmpassos (Priest) on Feb 21, 2005 at 00:26 UTC
    What you want is multithread. Take a look at threads.

    For multiple executions of multiple tasks you can use modules that help to do that with threads: Thread::Pool, Thread::Isolate::Pool.

    Note that you will need Perl-5.8.3+ to can use multithreading. For multithreading is recomended to always use the last Perl version, Perl-5.8.6, specially because 5.8.6 has some memory leak fixes.

    Graciliano M. P.
    "Creativity is the expression of liberty".

Re: Multitasking in Perl
by thekestrel (Friar) on Feb 21, 2005 at 23:48 UTC
    Hi,
    I have used POE quite sucessfully for making some 'multitasking' scripts. It basically runs its own mini multitasking kernel. There e a few threads in the archives at perlmonks if you sniff around.
    Regards Paul.