1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
[](https://codecov.io/github/openSUSE/Mojo-IOLoop-ReadWriteProcess?branch=master) [](https://github.com/openSUSE/Mojo-IOLoop-ReadWriteProcess/actions/workflows/ci-tests.yaml)
# NAME
Mojo::IOLoop::ReadWriteProcess - Execute external programs or internal code blocks as separate process.
# SYNOPSIS
use Mojo::IOLoop::ReadWriteProcess;
# Code fork
my $process = Mojo::IOLoop::ReadWriteProcess->new(sub { print "Hello\n" });
$process->start();
print "Running\n" if $process->is_running();
$process->getline(); # Will return "Hello\n"
$process->pid(); # Process id
$process->stop();
$process->wait_stop(); # if you intend to wait its lifespan
# Methods can be chained, thus this is valid:
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $output = process( sub { print "Hello\n" } )->start()->wait_stop->getline;
# Handles seamelessy also external processes:
my $process = process(execute=> '/path/to/bin' )->args([qw(foo bar baz)]);
$process->start();
my $line_output = $process->getline();
my $pid = $process->pid();
$process->stop();
my @errors = $process->error;
# To help when debugging Mojo::Collections
use Mojo::Util qw(dumper);
my $errors = dumper $process->error->to_array;
# Get process return value
$process = process( sub { return "256"; } )->start()->wait_stop;
# We need to stop it to retrieve the exit status
my $return = $process->return_status;
# We can access directly to handlers from the object:
my $stdout = $process->read_stream;
my $stdin = $process->write_stream;
my $stderr = $process->error_stream;
# So this works:
print $stdin "foo bar\n";
my @lines = <$stdout>;
# There is also an alternative channel of communication (just for forked processes):
my $channel_in = $process->channel_in; # write to the child process
my $channel_out = $process->channel_out; # read from the child process
$process->channel_write("PING"); # convenience function
# DESCRIPTION
Mojo::IOLoop::ReadWriteProcess is yet another process manager.
# EVENTS
[Mojo::IOLoop::ReadWriteProcess](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess) inherits all events from [Mojo::EventEmitter](https://metacpan.org/pod/Mojo%3A%3AEventEmitter) and can emit
the following new ones.
## start
$process->on(start => sub {
my ($process) = @_;
$process->is_running();
});
Emitted when the process starts.
## stop
$process->on(stop => sub {
my ($process) = @_;
$process->restart();
});
Emitted when the process stops.
## process\_error
$process->on(process_error => sub {
my ($e) = @_;
my @errors = @{$e};
});
Emitted when the process produce errors.
## process\_stuck
$process->on(process_stuck => sub {
my ($self) = @_;
...
});
Emitted when `blocking_stop` is set and all attempts for killing the process
in `max_kill_attempts` have been exhausted.
The event is emitted before attempting to kill it with SIGKILL and becoming blocking.
## SIG\_CHLD
$process->on(SIG_CHLD => sub {
my ($self) = @_;
...
});
Emitted when we receive SIG\_CHLD.
## SIG\_TERM
$process->on(SIG_TERM => sub {
my ($self) = @_;
...
});
Emitted when the child forked process receives SIG\_TERM, before exiting.
## collected
$process->on(collected => sub {
my ($self) = @_;
...
});
Emitted right after status collection.
## collect\_status
$process->on(collect_status => sub {
my ($self) = @_;
...
});
Emitted when on child process waitpid.
It is used internally to get the child process status.
Note: events attached to it are wiped when process has been stopped.
# ATTRIBUTES
[Mojo::IOLoop::ReadWriteProcess](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess) inherits all attributes from [Mojo::EventEmitter](https://metacpan.org/pod/Mojo%3A%3AEventEmitter) and implements
the following new ones.
## execute
use Mojo::IOLoop::ReadWriteProcess;
my $process = Mojo::IOLoop::ReadWriteProcess->new(execute => "/usr/bin/perl");
$process->start();
$process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
$process->stop();
`execute` should contain the external program that you wish to run.
## code
use Mojo::IOLoop::ReadWriteProcess;
my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello" } );
$process->start();
$process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
$process->stop();
It represent the code you want to run in background.
You do not need to specify `code`, it is implied if no arguments is given.
my $process = Mojo::IOLoop::ReadWriteProcess->new(sub { print "Hello" });
$process->start();
$process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
$process->stop();
## args
use Mojo::IOLoop::ReadWriteProcess;
my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello ".$_[1] }, args => "User" );
$process->start();
$process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
$process->stop();
# The process will print "Hello User"
Arguments pass to the external binary or the code block. Use arrayref to pass many.
## blocking\_stop
use Mojo::IOLoop::ReadWriteProcess;
my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello" }, blocking_stop => 1 );
$process->start();
$process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
$process->stop(); # Will wait indefinitely until the process is stopped
Set it to 1 if you want to do blocking stop of the process.
## channels
use Mojo::IOLoop::ReadWriteProcess;
my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello" }, channels => 0 );
$process->start();
$process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
$process->stop(); # Will wait indefinitely until the process is stopped
Set it to 0 if you want to disable internal channels.
## session
use Mojo::IOLoop::ReadWriteProcess;
my $process = Mojo::IOLoop::ReadWriteProcess->new(sub { print "Hello" });
my $session = $process->session;
$session->enable_subreaper;
Returns the current [Mojo::IOLoop::ReadWriteProcess::Session](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess%3A%3ASession) singleton.
## subreaper
use Mojo::IOLoop::ReadWriteProcess;
my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello ".$_[1] }, args => "User" );
$process->subreaper(1)->start();
$process->on( stop => sub { shift()->disable_subreaper } );
$process->stop();
# The process will print "Hello User"
Mark the current process (not the child) as subreaper on start.
It's on invoker behalf to disable subreaper when process stops, as it marks the current process and not the
child.
## ioloop
my $loop = $process->ioloop;
$subprocess = $process->ioloop(Mojo::IOLoop->new);
Event loop object to control, defaults to the global [Mojo::IOLoop](https://metacpan.org/pod/Mojo%3A%3AIOLoop) singleton.
## max\_kill\_attempts
use Mojo::IOLoop::ReadWriteProcess;
my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { print "Hello" }, max_kill_attempts => 50 );
$process->start();
$process->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
$process->stop(); # It will attempt to send SIGTERM 50 times.
Defaults to `5`, is the number of attempts before bailing out.
It can be used with blocking\_stop, so if the number of attempts are exhausted,
a SIGKILL and waitpid will be tried at the end.
## kill\_whole\_group
use Mojo::IOLoop::ReadWriteProcess;
my $process = Mojo::IOLoop::ReadWriteProcess->new(code => sub { setpgrp(0, 0); exec(...); }, kill_whole_group => 1 );
$process->start();
$process->send_signal(...); # Will skip the usual check whether $process->pid is running
$process->stop(); # Kills the entire process group and waits for all processes in the group to finish
Defaults to `0`, whether to send signals (e.g. to stop) to the entire process group.
This is useful when the sub process creates further sub processes and creates a new process
group as shown in the example. In this case it might be useful to take care of the entire process
group when stopping and wait for every process in the group to finish.
## collect\_status
Defaults to `1`, If enabled it will automatically collect the status of the children process.
Disable it in case you want to manage your process child directly, and do not want to rely on
automatic collect status. If you won't overwrite your `SIGCHLD` handler,
the `SIG_CHLD` event will be still emitted.
## serialize
Defaults to `0`, If enabled data returned from forked process will be serialized with Storable.
## kill\_sleeptime
Defaults to `1`, it's the seconds to wait before attempting SIGKILL when blocking\_stop is set to 1.
## separate\_err
Defaults to `1`, it will create a separate channel to intercept process STDERR,
otherwise it will be redirected to STDOUT.
## verbose
Defaults to `1`, it indicates message verbosity.
## set\_pipes
Defaults to `1`, If enabled, additional pipes for process communication are automatically set up.
## internal\_pipes
Defaults to `1`, If enabled, additional pipes for retreiving process return and errors are set up.
Note: If you disable that, the only information provided by the process will be the exit\_status.
## autoflush
Defaults to `1`, If enabled autoflush of handlers is enabled automatically.
## error
Returns a [Mojo::Collection](https://metacpan.org/pod/Mojo%3A%3ACollection) of errors.
Note: errors that can be captured only at the end of the process
# METHODS
[Mojo::IOLoop::ReadWriteProcess](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess) inherits all methods from [Mojo::EventEmitter](https://metacpan.org/pod/Mojo%3A%3AEventEmitter) and implements
the following new ones.
## start()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub {
print STDERR "Boo\n"
} )->start;
Starts the process
## stop()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process( execute => "/path/to/bin" )->start->stop;
Stop the process. Unless you use `wait_stop()`, it will attempt to kill the process
without waiting the process to finish. By defaults it send `SIGTERM` to the child.
You can change that by defining the internal attribute `_default_kill_signal`.
Note, if you want to be \*sure\* that the process gets killed, you can enable the
`blocking_stop` attribute, that will attempt to send `SIGKILL` after `max_kill_attempts`
is reached.
## restart()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process( execute => "/path/to/bin" )->restart;
It restarts the process if stopped, or if already running, it stops it first.
## is\_running()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process( execute => "/path/to/bin" )->start;
$p->is_running;
Boolean, it inspect if the process is currently running or not.
## exit\_status()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process( execute => "/path/to/bin" )->start;
$p->wait_stop->exit_status;
Inspect the process exit status, it does the shifting magic, to access to the real value
call `_status()`.
## return\_status()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process( sub { return 42 } )->start;
my $s = $p->wait_stop->return_status; # 42
Inspect the codeblock return.
## enable\_subreaper()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process()->enable_subreaper;
Mark the current process (not the child) as subreaper.
This is used typically if you want to mark further children as subreapers inside other forks.
my $master_p = process(
sub {
my $p = shift;
$p->enable_subreaper;
process(sub { sleep 4; exit 1 })->start();
process(
sub {
sleep 4;
process(sub { sleep 1; })->start();
})->start();
process(sub { sleep 4; exit 0 })->start();
process(sub { sleep 4; die })->start();
my $manager
= process(sub { sleep 2 })->subreaper(1)->start();
sleep 1 for (0 .. 10);
$manager->stop;
return $manager->session->all->size;
});
$master_p->subreaper(1);
$master_p->on(collected => sub { $status++ });
# On start we setup the current process as subreaper
# So it's up on us to disable it after process is done.
$master_p->on(stop => sub { shift()->disable_subreaper });
$master_p->start();
## disable\_subreaper()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process()->disable_subreaper;
Unset the current process (not the child) as subreaper.
## prctl()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process();
$p->prctl($option, $arg2, $arg3, $arg4, $arg5);
Internal function to execute and wrap the prctl syscall, accepts the same arguments as prctl.
## diag()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub { print "Hello\n" });
$p->on( stop => sub { shift->diag("Done!") } );
$p->start->wait_stop;
Internal function to print information to STDERR if verbose attribute is set or either DEBUG mode enabled.
You can use it if you wish to display information on the process status.
## to\_ioloop()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub { print "Hello from first process\n"; sleep 1 });
$p->start(); # Start and sets the handlers
my $stream = $p->to_ioloop; # Get the stream and demand to IOLoop
my $output;
# Hook on Mojo::IOLoop::Stream events
$stream->on(read => sub { $output .= pop; $p->is_running ... });
Mojo::IOLoop->singleton->start() unless Mojo::IOLoop->singleton->is_running;
Returns a [Mojo::IOLoop::Stream](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AStream) object and demand the wait operation to [Mojo::IOLoop](https://metacpan.org/pod/Mojo%3A%3AIOLoop).
It needs `set_pipes` enabled. Default IOLoop can be overridden in `ioloop()`.
## wait()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub { print "Hello\n" })->wait;
# ... here now you can mangle $p handlers and such
Waits until the process finishes, but does not performs cleanup operations (until stop is called).
## wait\_stop()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub { print "Hello\n" })->start->wait_stop;
# $p is not running anymore, and all possible events have been granted to be emitted.
Waits until the process finishes, and perform cleanup operations.
## errored()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub { die "Nooo" })->start->wait_stop;
$p->errored; # will return "1"
Returns a boolean indicating if the process had errors or not.
## write\_pidfile()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub { die "Nooo" } );
$p->pidfile("foobar");
$p->start();
$p->write_pidfile();
Forces writing PID of process to specified pidfile in the attributes of the object.
Useful only if the process have been already started, otherwise if a pidfile it's supplied
as attribute, it will be done automatically.
## write\_stdin()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub { my $a = <STDIN>; print STDERR "Hello my name is $a\n"; } )->start;
$p->write_stdin("Larry");
$p->read_stderr; # process STDERR will contain: "Hello my name is Larry\n"
Write data to process STDIN.
## write\_channel()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub {
my $self = shift;
my $parent_output = $self->channel_out;
my $parent_input = $self->channel_in;
while(defined(my $line = <$parent_input>)) {
print $parent_output "PONG\n" if $line =~ /PING/i;
}
} )->start;
$p->write_channel("PING");
my $out = $p->read_channel;
# $out is PONG
my $child_output = $p->channel_out;
while(defined(my $line = <$child_output>)) {
print "Process is replying back with $line!\n";
$p->write_channel("PING");
}
Write data to process channel. Note, it's not STDIN, neither STDOUT, it's a complete separate channel
dedicated to parent-child communication.
In the parent process, you can access to the same pipes (but from the opposite direction):
my $child_output = $self->channel_out;
my $child_input = $self->channel_in;
## read\_stdout()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub {
print "Boo\n"
} )->start;
$p->read_stdout;
Gets a single line from process STDOUT.
## read\_channel()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub {
my $self = shift;
my $parent_output = $self->channel_out;
my $parent_input = $self->channel_in;
print $parent_output "PONG\n";
} )->start;
$p->read_channel;
Gets a single line from process channel.
## read\_stderr()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub {
print STDERR "Boo\n"
} )->start;
$p->read_stderr;
Gets a single line from process STDERR.
## read\_all\_stdout()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub {
print "Boo\n"
} )->start;
$p->read_all_stdout;
Gets all the STDOUT output of the process.
## read\_all\_channel()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub {
shift->channel_out->write("Ping")
} )->start;
$p->read_all_channel;
Gets all the channel output of the process.
## read\_all\_stderr()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process(sub {
print STDERR "Boo\n"
} )->start;
$p->read_all_stderr;
Gets all the STDERR output of the process.
## send\_signal()
use Mojo::IOLoop::ReadWriteProcess qw(process);
use POSIX;
my $p = process( execute => "/path/to/bin" )->start;
$p->send_signal(POSIX::SIGKILL);
Send a signal to the process
# EXPORTS
## parallel()
use Mojo::IOLoop::ReadWriteProcess qw(parallel);
my $pool = parallel sub { print "Hello\n" } => 5;
$pool->start();
$pool->on( stop => sub { print "Process: ".(+shift()->pid)." finished"; } );
$pool->stop();
Returns a [Mojo::IOLoop::ReadWriteProcess::Pool](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess%3A%3APool) object that represent a group of processes.
It accepts the same arguments as [Mojo::IOLoop::ReadWriteProcess](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess), and the last one represent the number of processes to generate.
## batch()
use Mojo::IOLoop::ReadWriteProcess qw(batch);
my $pool = batch;
$pool->add(sub { print "Hello\n" });
$pool->on(stop => sub { shift->_diag("Done!") })->start->wait_stop;
Returns a [Mojo::IOLoop::ReadWriteProcess::Pool](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess%3A%3APool) object generated from supplied arguments.
It accepts as input the same parameter of [Mojo::IOLoop::ReadWriteProcess::Pool](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess%3A%3APool) constructor ( see parallel() ).
## process()
use Mojo::IOLoop::ReadWriteProcess qw(process);
my $p = process sub { print "Hello\n" };
$p->start()->wait_stop;
or even:
process(sub { print "Hello\n" })->start->wait_stop;
Returns a [Mojo::IOLoop::ReadWriteProcess](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess) object that represent a process.
It accepts the same arguments as [Mojo::IOLoop::ReadWriteProcess](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess).
## queue()
use Mojo::IOLoop::ReadWriteProcess qw(queue);
my $q = queue;
$q->add(sub { return 42 } );
$q->consume;
Returns a [Mojo::IOLoop::ReadWriteProcess::Queue](https://metacpan.org/pod/Mojo%3A%3AIOLoop%3A%3AReadWriteProcess%3A%3AQueue) object that represent a queue.
# DEBUGGING
You can set the MOJO\_EVENTEMITTER\_DEBUG environment variable to get some advanced diagnostics information printed to STDERR.
MOJO_EVENTEMITTER_DEBUG=1
Also, you can set MOJO\_PROCESS\_DEBUG environment variable to get diagnostics about the process execution.
MOJO_PROCESS_DEBUG=1
# LICENSE
Copyright (C) Ettore Di Giacinto.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
# AUTHOR
Ettore Di Giacinto <edigiacinto@suse.com>
|