.\" Automatically generated by Pod::Man 4.10 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::IOLoop::Subprocess 3pm" .TH Mojo::IOLoop::Subprocess 3pm "2019-02-05" "perl v5.28.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojo::IOLoop::Subprocess \- Subprocesses .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::IOLoop::Subprocess; \& \& # Operation that would block the event loop for 5 seconds \& my $subprocess = Mojo::IOLoop::Subprocess\->new; \& $subprocess\->run( \& sub { \& my $subprocess = shift; \& sleep 5; \& return \*(Aq♥\*(Aq, \*(AqMojolicious\*(Aq; \& }, \& 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; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Mojo::IOLoop::Subprocess allows Mojo::IOLoop to perform computationally expensive operations in subprocesses, without blocking the event loop. .SH "EVENTS" .IX Header "EVENTS" Mojo::IOLoop::Subprocess inherits all events from Mojo::EventEmitter and can emit the following new ones. .SS "progress" .IX Subsection "progress" .Vb 4 \& $subprocess\->on(progress => sub { \& my ($subprocess, @data) = @_; \& ... \& }); .Ve .PP Emitted in the parent process when the subprocess calls the progress method. .SS "spawn" .IX Subsection "spawn" .Vb 4 \& $subprocess\->on(spawn => sub { \& my $subprocess = shift; \& ... \& }); .Ve .PP Emitted in the parent process when the subprocess has been spawned. .PP .Vb 5 \& $subprocess\->on(spawn => sub { \& my $subprocess = shift; \& my $pid = $subprocess\->pid; \& say "Performing work in process $pid"; \& }); .Ve .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Mojo::IOLoop::Subprocess implements the following attributes. .SS "deserialize" .IX Subsection "deserialize" .Vb 2 \& my $cb = $subprocess\->deserialize; \& $subprocess = $subprocess\->deserialize(sub {...}); .Ve .PP A callback used to deserialize subprocess return values, defaults to using Storable. .PP .Vb 4 \& $subprocess\->deserialize(sub { \& my $bytes = shift; \& return []; \& }); .Ve .SS "ioloop" .IX Subsection "ioloop" .Vb 2 \& my $loop = $subprocess\->ioloop; \& $subprocess = $subprocess\->ioloop(Mojo::IOLoop\->new); .Ve .PP Event loop object to control, defaults to the global Mojo::IOLoop singleton. Note that this attribute is weakened. .SS "serialize" .IX Subsection "serialize" .Vb 2 \& my $cb = $subprocess\->serialize; \& $subprocess = $subprocess\->serialize(sub {...}); .Ve .PP A callback used to serialize subprocess return values, defaults to using Storable. .PP .Vb 4 \& $subprocess\->serialize(sub { \& my $array = shift; \& return \*(Aq\*(Aq; \& }); .Ve .SH "METHODS" .IX Header "METHODS" Mojo::IOLoop::Subprocess inherits all methods from Mojo::EventEmitter and implements the following new ones. .SS "pid" .IX Subsection "pid" .Vb 1 \& my $pid = $subprocess\->pid; .Ve .PP Process id of the spawned subprocess if available. .SS "progress" .IX Subsection "progress" .Vb 1 \& $subprocess\->progress(@data); .Ve .PP Send data serialized with Storable to the parent process at any time during the subprocess's execution. Must be called by the subprocess and emits the \&\*(L"progress\*(R" event in the parent process with the data. .PP .Vb 10 \& # Send progress information to the parent process \& $subprocess\->run( \& sub { \& my $subprocess = shift; \& $subprocess\->progress(\*(Aq0%\*(Aq); \& sleep 5; \& $subprocess\->progress(\*(Aq50%\*(Aq); \& sleep 5; \& return \*(AqHello Mojo!\*(Aq; \& }, \& sub { \& my ($subprocess, $err, @results) = @_; \& say \*(AqProgress is 100%\*(Aq; \& say $results[0]; \& } \& ); \& $subprocess\->on(progress => sub { \& my ($subprocess, @data) = @_; \& say "Progress is $data[0]"; \& }); .Ve .SS "run" .IX Subsection "run" .Vb 1 \& $subprocess = $subprocess\->run(sub {...}, sub {...}); .Ve .PP Execute the first callback in a child process and wait for it to return one or more values, without blocking \*(L"ioloop\*(R" in the parent process. Then execute the second callback in the parent process with the results. The return values of the first callback and exceptions thrown by it, will be serialized with Storable, so they can be shared between processes. .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .