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