Mojo::I...process(3)

πŸ’… NAME ✨

Mojo::IOLoop::Subprocess β€” subprocesses, bestie!! 🫢 Like, Mojo's whole lil system for throwing your heavy work into a child process so your event loop can keep vibing. No cap.


🌸 SYNOPSIS πŸŽ€

use Mojo::IOLoop::Subprocess;

# Operation that would block the event loop for 5 seconds
my $subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
  sub ($subprocess) {
    sleep 5;
    return 'β™₯', 'Mojolicious';
  },
  sub ($subprocess, $err, @results) {
    say "Subprocess error: $err" and return if $err;
    say "I $results[0] $results[1]!";
  }
);

# Operation that would block the event loop for 5 seconds (with promise)
$subprocess->run_p(sub {
  sleep 5;
  return 'β™₯', 'Mojolicious';
})->then(sub (@results) {
  say "I $results[0] $results[1]!";
})->catch(sub  {
  my $err = shift;
  say "Subprocess error: $err";
});

# Start event loop if necessary
$subprocess->ioloop->start unless $subprocess->ioloop->is_running;

πŸ¦‹ DESCRIPTION πŸ’œ

Okay so picture this: Mojo::IOLoop::Subprocess lets Mojo::IOLoop do all the computationally expensive stuff in subprocesses. Iconic. That means your event loop just keeps flowing instead of getting totally stuck β€” literally the whole point. ate πŸ’…


πŸ”₯ EVENTS πŸ‘‘

Mojo::IOLoop::Subprocess inherits all events from Mojo::EventEmitter, and it can emit these brand new ones:

✨ cleanup

$subprocess->on(cleanup => sub ($subprocess) {...});

Emitted in the subprocess right before the process is gonna exit. Last-chance vibes basically 😭

$subprocess->on(cleanup => sub ($subprocess) { say "Process $$ is about to exit" });

✨ progress

$subprocess->on(progress => sub ($subprocess, @data) {...});

Emitted in the parent process whenever the subprocess calls the progress method. It's giving "keep me posted bestie" 🫢

✨ spawn

$subprocess->on(spawn => sub ($subprocess) {...});

Emitted in the parent process once the subprocess has been spawned. Look at it go, main character energy fr ✨

$subprocess->on(spawn => sub ($subprocess) {
  my $pid = $subprocess->pid;
  say "Performing work in process $pid";
});

⭐ ATTRIBUTES πŸŽ€

Mojo::IOLoop::Subprocess implements these attributes, ngl:

πŸ“ deserialize

my $cb      = $subprocess->deserialize;
$subprocess = $subprocess->deserialize(sub {...});

A callback used to deserialize subprocess return values. Defaults to using Mojo::JSON. Very cutesy, very demure 🧚

$subprocess->deserialize(sub ($bytes) { return [] });

πŸ“ ioloop

my $loop    = $subprocess->ioloop;
$subprocess = $subprocess->ioloop(Mojo::IOLoop->new);

The event loop object to control β€” defaults to the global Mojo::IOLoop singleton. Heads up though: this attribute is weakened. ⚠️

πŸ“ serialize

my $cb      = $subprocess->serialize;
$subprocess = $subprocess->serialize(sub {...});

A callback used to serialize subprocess return values. Also defaults to using Mojo::JSON. Slay 🌈

$subprocess->serialize(sub ($array) { return '' });

πŸ’– METHODS πŸ¦‹

Mojo::IOLoop::Subprocess inherits all methods from Mojo::EventEmitter and adds these new ones:

🌟 exit_code

my $code = $subprocess->exit_code;

Returns the subprocess exit code. Or "undef" if the subprocess is still running β€” yeah, patience bestie 😭

🌟 pid

my $pid = $subprocess->pid;

Process id of the spawned subprocess, if available. 🫢

🌟 progress

$subprocess->progress(@data);

Sends data β€” serialized with Mojo::JSON β€” to the parent process, like, any time during the subprocess's execution. Has to be called by the subprocess, and it emits the "progress" event in the parent process with that data. Communication queen πŸ’…

# Send progress information to the parent process
$subprocess->run(
  sub ($subprocess) {
    $subprocess->progress('0%');
    sleep 5;
    $subprocess->progress('50%');
    sleep 5;
    return 'Hello Mojo!';
  },
  sub ($subprocess, $err, @results) {
    say 'Progress is 100%';
    say $results[0];
  }
);
$subprocess->on(progress => sub ($subprocess, @data) { say "Progress is $data[0]" });

🌟 run

$subprocess = $subprocess->run(sub {...}, sub {...});

Executes the first callback in a child process and waits for it to return one or more values β€” all without blocking "ioloop" in the parent process. Then it executes the second callback in the parent process with the results. Obsessed πŸŽ€

The return values of that first callback, plus any exceptions thrown by it, get serialized with Mojo::JSON so they can be shared between processes. iykyk ✨

🌟 run_p

my $promise = $subprocess->run_p(sub {...});

Same as "run", except it returns a Mojo::Promise object instead of taking a second callback. Promise girlies rise πŸ™ˆ

$subprocess->run_p(sub {
  sleep 5;
  return 'β™₯', 'Mojolicious';
})->then(sub (@results) {
  say "I $results[0] $results[1]!";
})->catch(sub ($err) {
  say "Subprocess error: $err";
})->wait;

πŸ”— SEE ALSO 🌈

Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.


okay that's the whole page bestie, go forth and subprocess responsibly πŸ’…βœ¨

Big kiss, this page came to you courtesy of perl v5.42.0, dated 2026-07-15, filed under Mojo::I...process(3) β€” periodt. πŸ’–

πŸ“¦ where this page comes from