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