1Mojo::IOLoop::SubprocesUss(e3r)Contributed Perl DocumentMaotjioo:n:IOLoop::Subprocess(3)
2
3
4

NAME

6       Mojo::IOLoop::Subprocess - Subprocesses
7

SYNOPSIS

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

DESCRIPTION

30       Mojo::IOLoop::Subprocess allows Mojo::IOLoop to perform computationally
31       expensive operations in subprocesses, without blocking the event loop.
32

EVENTS

34       Mojo::IOLoop::Subprocess inherits all events from Mojo::EventEmitter
35       and can emit the following new ones.
36
37   progress
38         $subprocess->on(progress => sub {
39           my ($subprocess, @data) = @_;
40           ...
41         });
42
43       Emitted in the parent process when the subprocess calls the progress
44       method.
45
46   spawn
47         $subprocess->on(spawn => sub {
48           my $subprocess = shift;
49           ...
50         });
51
52       Emitted in the parent process when the subprocess has been spawned.
53
54         $subprocess->on(spawn => sub {
55           my $subprocess = shift;
56           my $pid = $subprocess->pid;
57           say "Performing work in process $pid";
58         });
59

ATTRIBUTES

61       Mojo::IOLoop::Subprocess implements the following attributes.
62
63   deserialize
64         my $cb      = $subprocess->deserialize;
65         $subprocess = $subprocess->deserialize(sub {...});
66
67       A callback used to deserialize subprocess return values, defaults to
68       using Storable.
69
70         $subprocess->deserialize(sub {
71           my $bytes = shift;
72           return [];
73         });
74
75   ioloop
76         my $loop    = $subprocess->ioloop;
77         $subprocess = $subprocess->ioloop(Mojo::IOLoop->new);
78
79       Event loop object to control, defaults to the global Mojo::IOLoop
80       singleton.  Note that this attribute is weakened.
81
82   serialize
83         my $cb      = $subprocess->serialize;
84         $subprocess = $subprocess->serialize(sub {...});
85
86       A callback used to serialize subprocess return values, defaults to
87       using Storable.
88
89         $subprocess->serialize(sub {
90           my $array = shift;
91           return '';
92         });
93

METHODS

95       Mojo::IOLoop::Subprocess inherits all methods from Mojo::EventEmitter
96       and implements the following new ones.
97
98   pid
99         my $pid = $subprocess->pid;
100
101       Process id of the spawned subprocess if available.
102
103   progress
104         $subprocess->progress(@data);
105
106       Send data serialized with Storable to the parent process at any time
107       during the subprocess's execution. Must be called by the subprocess and
108       emits the "progress" event in the parent process with the data.
109
110         # Send progress information to the parent process
111         $subprocess->run(
112           sub {
113             my $subprocess = shift;
114             $subprocess->progress('0%');
115             sleep 5;
116             $subprocess->progress('50%');
117             sleep 5;
118             return 'Hello Mojo!';
119           },
120           sub {
121             my ($subprocess, $err, @results) = @_;
122             say 'Progress is 100%';
123             say $results[0];
124           }
125         );
126         $subprocess->on(progress => sub {
127           my ($subprocess, @data) = @_;
128           say "Progress is $data[0]";
129         });
130
131   run
132         $subprocess = $subprocess->run(sub {...}, sub {...});
133
134       Execute the first callback in a child process and wait for it to return
135       one or more values, without blocking "ioloop" in the parent process.
136       Then execute the second callback in the parent process with the
137       results. The return values of the first callback and exceptions thrown
138       by it, will be serialized with Storable, so they can be shared between
139       processes.
140

SEE ALSO

142       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
143
144
145
146perl v5.28.0                      2018-11-08       Mojo::IOLoop::Subprocess(3)
Impressum