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

Hi,monks.
Could you please provide me a small code example of how to
implement multithreading in perl.
I'm trying to write a script that downloads file via ftp
and at the same time prints '#' on STDOUT until the
download is finished.
Thank you very much in advance!

Replies are listed 'Best First'.
Re: multithreading sample
by BrowserUk (Patriarch) on Aug 07, 2005 at 08:39 UTC

    If you use Net::FTP there is no need for any multitasking. Just supply Hash => \*STDOUT option to the constructor and the module will do it for you.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: multithreading sample
by fmerges (Chaplain) on Aug 07, 2005 at 13:24 UTC

    Hi,

    Here you have an example:

    #!/usr/bin/perl -w $|++; use strict; use threads; use threads::shared; # Flag variable we use to stop the 2nd thread my $complete : shared = 0; # Create the thread my $thread = threads->create(\&dothis); # Code of main thread goes here sleep 5; # Change the flag variable so the thread ends $complete = 1; # Join the threads together $thread->join(); # End of the program exit; sub dothis { until($complete) { print "#"; sleep 1; } print "\n"; }

    Take a look at threads.

    But you can use fork and sending a kill signal to the child instead, I think it would be faster and more efficient.

    For more complicated solution think about Event, Event::Lib or POE.

    Update: This code is a example of threads, but of course for a simple stats of a file download is not necessary... as stated before. I supossed that you wanted a simple example to see how threads work in Perl.

    Regards,

    |fire| at irc.freenode.net
Re: multithreading sample
by fizbin (Chaplain) on Aug 07, 2005 at 15:17 UTC
    Although other people have shown you different ways in which to be multithreaded, I'd like to point out that the task you mentioned (FTP with hashmark printing) does not sound like something where you need or even want multithreading at all.

    Multithreading is necessary when you need separate computations to happen at the same time (e.g. on a multi-cpu machine) (*). Multithreading is occasionally useful when you have to monitor multiple different input streams or monitor an input stream while doing some complex calculations in the background. (Some people just don't like writing state machines or using POE) The problem you mention sounds nothing like either of these cases.

    To approach the problem you mentioned, it seems to that the matter is simple: pull down some of the file, see if you've pulled down enough bytes to write another '#' mark, print it if so, and repeat. Of course, if you're using Net::FTP to pull the file down (you aren't rewriting your own ftp protocol, are you?) then another poster already mentioned the option that'll do that for you. If you're running an external ftp program in the background, I'd suggest that you need to read perlipc and consider inter-process communication, and then check if your ftp program has the ability to print out hash marks as it's working (many do).

    (*) Yes, I'm aware that multithreading per se isn't the only way to split computations across multiple CPUs; obviously one can run entirely separate processes.

    -- @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: multithreading sample
by sk (Curate) on Aug 07, 2005 at 07:43 UTC
    I am not sure if this is too much for the task you are looking for

    But here are some parallel processing modules

    Parallel::ForkManager

    Proc::Queue

    Sample codes are available in the docs

    -SK

Re: multithreading sample
by castaway (Parson) on Aug 07, 2005 at 15:21 UTC
    Here's a somewhat longer example/tutorial: Going Up?.

    However you may not need multithreading, care to explain more what you want to do?

    C.