1Mojo::IOLoop::SubprocesUss(e3r)Contributed Perl DocumentMaotjioo:n:IOLoop::Subprocess(3)
2
3
4
6 Mojo::IOLoop::Subprocess - Subprocesses
7
9 use Mojo::IOLoop::Subprocess;
10
11 # Operation that would block the event loop for 5 seconds
12 my $subprocess = Mojo::IOLoop::Subprocess->new;
13 $subprocess->run(
14 sub {
15 my $subprocess = shift;
16 sleep 5;
17 return '♥', 'Mojolicious';
18 },
19 sub {
20 my ($subprocess, $err, @results) = @_;
21 say "Subprocess error: $err" and return if $err;
22 say "I $results[0] $results[1]!";
23 }
24 );
25
26 # Operation that would block the event loop for 5 seconds (with promise)
27 $subprocess->run_p(sub {
28 sleep 5;
29 return '♥', 'Mojolicious';
30 })->then(sub {
31 my @results = @_;
32 say "I $results[0] $results[1]!";
33 })->catch(sub {
34 my $err = shift;
35 say "Subprocess error: $err";
36 });
37
38 # Start event loop if necessary
39 $subprocess->ioloop->start unless $subprocess->ioloop->is_running;
40
42 Mojo::IOLoop::Subprocess allows Mojo::IOLoop to perform computationally
43 expensive operations in subprocesses, without blocking the event loop.
44
46 Mojo::IOLoop::Subprocess inherits all events from Mojo::EventEmitter
47 and can emit the following new ones.
48
49 cleanup
50 $subprocess->on(cleanup => sub {
51 my $subprocess = shift;
52 ...
53 });
54
55 Emitted in the subprocess right before the process will exit.
56
57 $subprocess->on(cleanup => sub {
58 my $subprocess = shift;
59 say "Process $$ is about to exit";
60 });
61
62 progress
63 $subprocess->on(progress => sub {
64 my ($subprocess, @data) = @_;
65 ...
66 });
67
68 Emitted in the parent process when the subprocess calls the progress
69 method.
70
71 spawn
72 $subprocess->on(spawn => sub {
73 my $subprocess = shift;
74 ...
75 });
76
77 Emitted in the parent process when the subprocess has been spawned.
78
79 $subprocess->on(spawn => sub {
80 my $subprocess = shift;
81 my $pid = $subprocess->pid;
82 say "Performing work in process $pid";
83 });
84
86 Mojo::IOLoop::Subprocess implements the following attributes.
87
88 deserialize
89 my $cb = $subprocess->deserialize;
90 $subprocess = $subprocess->deserialize(sub {...});
91
92 A callback used to deserialize subprocess return values, defaults to
93 using Mojo::JSON.
94
95 $subprocess->deserialize(sub {
96 my $bytes = shift;
97 return [];
98 });
99
100 ioloop
101 my $loop = $subprocess->ioloop;
102 $subprocess = $subprocess->ioloop(Mojo::IOLoop->new);
103
104 Event loop object to control, defaults to the global Mojo::IOLoop
105 singleton. Note that this attribute is weakened.
106
107 serialize
108 my $cb = $subprocess->serialize;
109 $subprocess = $subprocess->serialize(sub {...});
110
111 A callback used to serialize subprocess return values, defaults to
112 using Mojo::JSON.
113
114 $subprocess->serialize(sub {
115 my $array = shift;
116 return '';
117 });
118
120 Mojo::IOLoop::Subprocess inherits all methods from Mojo::EventEmitter
121 and implements the following new ones.
122
123 exit_code
124 my $code = $subprocess->exit_code;
125
126 Returns the subprocess exit code, or "undef" if the subprocess is still
127 running.
128
129 pid
130 my $pid = $subprocess->pid;
131
132 Process id of the spawned subprocess if available.
133
134 progress
135 $subprocess->progress(@data);
136
137 Send data serialized with Mojo::JSON to the parent process at any time
138 during the subprocess's execution. Must be called by the subprocess and
139 emits the "progress" event in the parent process with the data.
140
141 # Send progress information to the parent process
142 $subprocess->run(
143 sub {
144 my $subprocess = shift;
145 $subprocess->progress('0%');
146 sleep 5;
147 $subprocess->progress('50%');
148 sleep 5;
149 return 'Hello Mojo!';
150 },
151 sub {
152 my ($subprocess, $err, @results) = @_;
153 say 'Progress is 100%';
154 say $results[0];
155 }
156 );
157 $subprocess->on(progress => sub {
158 my ($subprocess, @data) = @_;
159 say "Progress is $data[0]";
160 });
161
162 run
163 $subprocess = $subprocess->run(sub {...}, sub {...});
164
165 Execute the first callback in a child process and wait for it to return
166 one or more values, without blocking "ioloop" in the parent process.
167 Then execute the second callback in the parent process with the
168 results. The return values of the first callback and exceptions thrown
169 by it, will be serialized with Mojo::JSON, so they can be shared
170 between processes.
171
172 run_p
173 my $promise = $subprocess->run_p(sub {...});
174
175 Same as "run", but returns a Mojo::Promise object instead of accepting
176 a second callback.
177
178 $subprocess->run_p(sub {
179 sleep 5;
180 return '♥', 'Mojolicious';
181 })->then(sub {
182 my @results = @_;
183 say "I $results[0] $results[1]!";
184 })->catch(sub {
185 my $err = shift;
186 say "Subprocess error: $err";
187 })->wait;
188
190 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
191
192
193
194perl v5.32.0 2020-07-28 Mojo::IOLoop::Subprocess(3)