Multithreading

From RAJ INFO
Jump to navigation Jump to search

This is really cool tool

  • Install the Library Parallel::ForkManager through CPAN
  • It works only in a loop (of anykind). so make a loop if you don't have one
  • Code as below
# Libraries
	use Parallel::ForkManager;

# My Variables
       my $pm = new Parallel::ForkManager(4); # 4 is the number of cores in your processor


# Loop

	foreach my $file(@files) {
		$i++;
		$pm->start and next;
                # Do your big process. There can be sub loops here
		$pm->finish;
	}
	$pm->wait_all_children;

# END