1Mojo::Reactor(3) User Contributed Perl Documentation Mojo::Reactor(3)
2
3
4
6 Mojo::Reactor - Low-level event reactor base class
7
9 package Mojo::Reactor::MyEventLoop;
10 use Mojo::Base 'Mojo::Reactor';
11
12 sub again {...}
13 sub io {...}
14 sub is_running {...}
15 sub next_tick {...}
16 sub one_tick {...}
17 sub recurring {...}
18 sub remove {...}
19 sub reset {...}
20 sub start {...}
21 sub stop {...}
22 sub timer {...}
23 sub watch {...}
24
26 Mojo::Reactor is an abstract base class for low-level event reactors,
27 like Mojo::Reactor::EV and Mojo::Reactor::Poll.
28
30 Mojo::Reactor inherits all events from Mojo::EventEmitter and can emit
31 the following new ones.
32
33 error
34 $reactor->on(error => sub {
35 my ($reactor, $err) = @_;
36 ...
37 });
38
39 Emitted for exceptions caught in callbacks, fatal if unhandled. Note
40 that if this event is unhandled or fails it might kill your program, so
41 you need to be careful.
42
43 $reactor->on(error => sub {
44 my ($reactor, $err) = @_;
45 say "Something very bad happened: $err";
46 });
47
49 Mojo::Reactor inherits all methods from Mojo::EventEmitter and
50 implements the following new ones.
51
52 again
53 $reactor->again($id);
54
55 Restart timer. Meant to be overloaded in a subclass. Note that this
56 method requires an active timer.
57
58 detect
59 my $class = Mojo::Reactor->detect;
60
61 Detect and load the best reactor implementation available, will try the
62 value of the "MOJO_REACTOR" environment variable, Mojo::Reactor::EV or
63 Mojo::Reactor::Poll.
64
65 # Instantiate best reactor implementation available
66 my $reactor = Mojo::Reactor->detect->new;
67
68 io
69 $reactor = $reactor->io($handle => sub {...});
70
71 Watch handle for I/O events, invoking the callback whenever handle
72 becomes readable or writable. Meant to be overloaded in a subclass.
73
74 # Callback will be executed twice if handle becomes readable and writable
75 $reactor->io($handle => sub {
76 my ($reactor, $writable) = @_;
77 say $writable ? 'Handle is writable' : 'Handle is readable';
78 });
79
80 is_running
81 my $bool = $reactor->is_running;
82
83 Check if reactor is running. Meant to be overloaded in a subclass.
84
85 next_tick
86 my $undef = $reactor->next_tick(sub {...});
87
88 Execute callback as soon as possible, but not before returning or other
89 callbacks that have been registered with this method, always returns
90 "undef". Meant to be overloaded in a subclass.
91
92 one_tick
93 $reactor->one_tick;
94
95 Run reactor until an event occurs. Note that this method can recurse
96 back into the reactor, so you need to be careful. Meant to be
97 overloaded in a subclass.
98
99 # Don't block longer than 0.5 seconds
100 my $id = $reactor->timer(0.5 => sub {});
101 $reactor->one_tick;
102 $reactor->remove($id);
103
104 recurring
105 my $id = $reactor->recurring(0.25 => sub {...});
106
107 Create a new recurring timer, invoking the callback repeatedly after a
108 given amount of time in seconds. Meant to be overloaded in a subclass.
109
110 remove
111 my $bool = $reactor->remove($handle);
112 my $bool = $reactor->remove($id);
113
114 Remove handle or timer. Meant to be overloaded in a subclass.
115
116 reset
117 $reactor->reset;
118
119 Remove all handles and timers. Meant to be overloaded in a subclass.
120
121 start
122 $reactor->start;
123
124 Start watching for I/O and timer events, this will block until "stop"
125 is called. Note that some reactors stop automatically if there are no
126 events being watched anymore. Meant to be overloaded in a subclass.
127
128 # Start reactor only if it is not running already
129 $reactor->start unless $reactor->is_running;
130
131 stop
132 $reactor->stop;
133
134 Stop watching for I/O and timer events. Meant to be overloaded in a
135 subclass.
136
137 timer
138 my $id = $reactor->timer(0.5 => sub {...});
139
140 Create a new timer, invoking the callback after a given amount of time
141 in seconds. Meant to be overloaded in a subclass.
142
143 watch
144 $reactor = $reactor->watch($handle, $readable, $writable);
145
146 Change I/O events to watch handle for with true and false values. Meant
147 to be overloaded in a subclass. Note that this method requires an
148 active I/O watcher.
149
150 # Watch only for readable events
151 $reactor->watch($handle, 1, 0);
152
153 # Watch only for writable events
154 $reactor->watch($handle, 0, 1);
155
156 # Watch for readable and writable events
157 $reactor->watch($handle, 1, 1);
158
159 # Pause watching for events
160 $reactor->watch($handle, 0, 0);
161
163 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
164
165
166
167perl v5.30.0 2019-07-26 Mojo::Reactor(3)