The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojo::IOLoop::Thread - Threaded Replacement for Mojo::IOLoop::Subprocess

SYNOPSIS

  use Mojo::IOLoop::Thread;

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

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

or

  use Mojo::IOLoop;
  use Mojo::IOLoop::Thread;

  my $iol = Mojo::IOLoop->new;
  $iol->subprocess(
    sub {'♥'},
    sub {
      my ($subprocess, $err, @results) = @_;
      say "Subprocess error: $err" and return if $err;
      say @results;
    }
  );
  $loop->start;

DESCRIPTION

Mojo::IOLoop::Thread is a multithreaded alternative for Mojo::IOLoop::Subprocess which is not available under Win32. It is a dropin replacement, takes the same parameters and works analoguous by just using threads instead of forked processes.

Mojo::IOLoop::Thread replaces "subprocess" in Mojo::IOLoop with a threaded version on module load.

BUGS and LIMITATIONS

exit_code will not work like "exit_code" in Mojo::IOLoop::Subprocess because threads are not able to pass the exit code back when they are joined by the main thread. Additionally the exit code will be set when the subprocess is run with run_p (see "run_p" in Mojo::IOLoop::Subprocess)

AUTHOR

Thomas Kratz <tomk@cpan.org>

REPOSITORY

https://github.com/tomk3003/mojo-ioloop-thread

COPYRIGHT

Copyright 2017-21 Thomas Kratz.

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.