Skip to content

Instantly share code, notes, and snippets.

@manwar
Last active March 1, 2025 16:45
Multi processing in Perl and Python.

-- Back to Index --


In the post, I showed how to do multi-threading in Perl and Python.

Let's try multi-processing in Perl first:

[Source Code]

#!/usr/bin/env perl

use v5.38;

sub worker($name) {
    say "Child process $name (PID: $$) started.";
    sleep(2);
    say "Child process $name (PID: $$) finished.";
}

say "Parent process PID: $$\n";
say "Starting 5 processes.";
my @child_pids;
for my $i (1..5) {
    my $pid = fork();

    if (!defined $pid) {
        die "Failed to fork: $!";
    } elsif ($pid == 0) {
        worker($i);
        exit;
    } else {
        push @child_pids, $pid;
    }
}

waitpid($_, 0) foreach @child_pids;

Time to see in action.

$ perl multi-processing.pl
Parent process PID: 2207

Starting 5 processes.
Child process 1 (PID: 2208) started.
Child process 2 (PID: 2209) started.
Child process 3 (PID: 2210) started.
Child process 4 (PID: 2211) started.
Child process 5 (PID: 2212) started.
Child process 5 (PID: 2212) finished.
Child process 1 (PID: 2208) finished.
Child process 2 (PID: 2209) finished.
Child process 3 (PID: 2210) finished.
Child process 4 (PID: 2211) finished.

The above Perl code can be improved with the use of CPAN module MCE as below:

[Source Code]

#!/usr/bin/env perl

use v5.38;
use MCE;

say "Parent process PID: $$\n";
say "Starting 5 processes.";
my $mce = MCE->new(
   max_workers => 5,
   user_func => sub {
      my ($mce) = @_;
      $mce->say("Child process " . $mce->wid . " (PID:$$) started.");
      sleep(2);
      $mce->say("Child process " . $mce->wid . " (PID: $$) finished.");
   }
);
$mce->run;

One more attempt this time with CPAN module Parallel::ForkManager.

[Source Code]

#!/usr/bin/env perl

use v5.38;
use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new(5);

$pm->run_on_finish(sub {
    my ($pid, $exit_code) = @_;
    if ($exit_code == 0) {
        say "Child process $pid finished.";
    }
});

say "Parent process PID: $$\n";
say "Starting 5 processes.";
foreach (1..5) {
    $pm->start and next;

    say "Child process $$ started.";
    sleep(2);

    $pm->finish(0);
}

$pm->wait_all_children;

Now time to do the same in Python.

[Source Code]

#!/usr/bin/env python3

import os
import time
import multiprocessing as mp

def worker():
    pid = os.getpid()
    print("Process " + str(pid) + " started.")
    time.sleep(2)
    print("Process " + str(pid) + " finished.")

if __name__ == '__main__':
    print('Process ID: ', os.getpid())
    print('\nStarting 5 processes.')
    for i in range(5):
        mp.Process(target=worker).start()

Let's run the code now:

$ python3 multi-processing.py
Process ID:  2213

Starting 5 processes.
Process 2214 started.
Process 2215 started.
Process 2216 started.
Process 2218 started.
Process 2217 started.
Process 2214 finished.
Process 2216 finished.
Process 2218 finished.
Process 2215 finished.
Process 2217 finished.

-- Back to Index --


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment