Item Description: Create and manage processes on Windows & Unix
Review Synopsis:
Review of Proc::Background
Ah, the subject of processes and forking. This brings up many questions in the monastery, so I decided I would add this to the module reviews section each time it came up. Yes, folks, spawning processes and waiting on them doesn't have to be so complicated. Let's look at the problem-space a bit and we will understand why Proc::Background is so cool to have.
The Problem
unix: There are several ways to background a process on Unix, one can fork and exec a system call, one can make a system call using &, and so on. There are also things like Proc::ProcessTable, but again, this can get complicated.
windows: There are also several ways to background a process on Windows, but forking is often implemented wrong on your particular version and should be avoided. There is Win32::Process, but it requires an absolute path to the executable you are trying to run, which can sometimes be hard to acquire when you don't know where that program lies in your path.
The Solution
The above problems can get ugly in a hurry, especially if you are new to Unix (and don't understand fork well), don't want to deal with Win32 modules, or if you want code that works in something other than Unixy environments. This is where Proc::Background comes in. It allows one to not worry about Unix or Windows and to (effectively) manage processes without all of the gory details. In addition, it allows waiting on arbitrary processes and checking on the status of each. Very cool, and even cooler because it is cross platform.
Example Code (borrowed from CPAN)
use Proc::Background; my $proc1 = Proc::Background->new($command, $arg1, $arg2); my $proc2 = Proc::Background->new("$command $arg1 1>&2"); $proc1->alive; $proc1->die; $proc1->wait;
see CPAN for the full list of functions, but those are the basics. Easy, no?
When To Use It
- When you want clean code that is very short and understandable
- When you must execute processes in cross-platform code for Windows and Unix
- When you don't have enough tylenol/advil/beer to deal with Win32::Process
- When you have to inquire about the status of arbitrary processes or must act upon their states (is process A up? How about B? Now wait for C to finish!)
When Not To Use It
- When you have Unix buddies you are trying to impress
- When you have Win32 buddies you are trying to impress
- When you are trying to keep up your job security by keeping code hard to read :)
- When you need to execute arbitrary Perl code and not seperate executables
- If you are using solaris or cygwin. It appears that (per CPAN reports) this may not work there. Your mileage may vary. (I use Linux and Win32 most of the time).
- Foreground processes? NO, WAIT! It works there too, just use 'wait' method on your process after you invoke it and you have something a little more spiffy than the stock system call. This is at your discretion, of course, this isn't really required.
The Final Word
This module is very useful and is currently in my top 10. It efficiently allows management of any number of processes and allows me to forget (when I feel like it) how fork, exec, and Win32::Process work in Perl -- saving me pain and frustration. It also makes code much more legible due to a clean API, and that is always a good thing in a module.You can remember how to use it without looking things up, since the API is so basic -- this is goodness. Try it out, unless you are a Unix purist who must always write their own fork code to spawn processes, this should work great for you.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Review of Proc::Background
by diotalevi (Canon) on Mar 24, 2004 at 22:53 UTC | |
by flyingmoose (Priest) on Mar 24, 2004 at 23:01 UTC | |
by diotalevi (Canon) on Mar 24, 2004 at 23:10 UTC | |
by flyingmoose (Priest) on Mar 25, 2004 at 01:30 UTC | |
by diotalevi (Canon) on Mar 25, 2004 at 01:59 UTC | |
|