Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How to make my script to more than one thing at once?

by 2mths (Beadle)
on Aug 14, 2003 at 09:35 UTC ( [id://283800]=perlquestion: print w/replies, xml ) Need Help??

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

Sorry - Rubbish title but I couldn't think of a better one.

I have a script which takes a list of machines on a network and distributes Anti-Virus updates to them. It works but it's very slow. Is there a way I can 'start' the process of updating a client (copy a package and execute it through OS commands) but not have the scripts sit there until it's finished before it caries on and starts the next one?

I've looked at exec but I don't think that's what I need. I tried looking at forking and I couldn't understand enough to work out if it was the answer or not.

Can anyone give me a pointer?
I've got Programming Perl and Perl Cookbook so feel free to direct me to a section in those.
Sorry if this has been asked before, but I couldn't write a good enough search to find anything useful.

Replies are listed 'Best First'.
Re: How to make my script to more than one thing at once?
by PodMaster (Abbot) on Aug 14, 2003 at 09:58 UTC
    Yes, forking should do, try out Parallel::ForkManager.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: How to make my script to more than one thing at once?
by Abigail-II (Bishop) on Aug 14, 2003 at 09:45 UTC
    forking (or threading) is the answer.

    Abigail

Re: How to make my script to more than one thing at once?
by edan (Curate) on Aug 14, 2003 at 10:57 UTC

    You don't mention what platform you're on, but here are some alternatives to forking depending on your needs:

    If you're on Unix/Linux, and the 'process' you need to start is an external command/program/script, you can just run it in the background using system("$cmd &").

    If you're on Windoze, I have heard you can do this to start a program in the background if you don't mind if it has it's own DOS window: system("start $cmd").

    Otherwise, use fork, unless you're on Windoze and fork doesn't work there, in which case, look here.

    HTH

    --
    3dan
Re: How to make my script to more than one thing at once?
by jonnyfolk (Vicar) on Aug 14, 2003 at 10:39 UTC
    Reading between the lines you might do well to run a search on Google for perl tutorial fork for some background reading on this.

    BTW IMHO tho' you have purchased worthy and excellent tomes you would probably get more out of them if you added merlyn's excellent Learning Perl to your collection. Oh, and reading it :)

Re: How to make... - Thankyou people
by 2mths (Beadle) on Aug 14, 2003 at 10:58 UTC
    I feared Fork would be the answer. Having stared at the books a while it still doesn't make much sense. However I have a feeling that to do what I want I'd need to fork hundereds of times and I think that would be bad form.

    I guess what I'm really after is a true 'fire and forget' entity which Perl doesn't have ('cos it's too responsible). Looks like I might have to use a batch script Yuk

    I'd love to add another book to the shelf but I couldn't really justify it as I've still to read the ones I've got. I'll bare it in mind though if some kind sole suggests we need some more reference material.

      fork works fine under Win32, but if you want to use system and don't want lots of CMD windows, just use the /B (for background) option on the start command and /c on cmd to make the cmd sessions 'go away' of their own volition.

      system( 'start /b cmd /c your_command' );

      If you have 5.8 then you can also use threads, the  async() function makes this very easy. The following line will do a dir for each subdirectory below your current directory asynchronously.

      use threads; async( sub{ system "dir $_" } ) for grep -d, glob '.\*';

      Not a very convincing demo, but it does work.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://283800]
Approved by gellyfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-28 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found