1Mojo::Reactor(3)      User Contributed Perl Documentation     Mojo::Reactor(3)
2
3
4

NAME

6       Mojo::Reactor - Low-level event reactor base class
7

SYNOPSIS

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

DESCRIPTION

26       Mojo::Reactor is an abstract base class for low-level event reactors,
27       like Mojo::Reactor::EV and Mojo::Reactor::Poll.
28

EVENTS

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

METHODS

49       Mojo::Reactor inherits all methods from Mojo::EventEmitter and
50       implements the following new ones.
51
52   again
53         $reactor->again($id);
54         $reactor->again($id, 0.5);
55
56       Restart timer and optionally change the invocation time. Meant to be
57       overloaded in a subclass. Note that this method requires an active
58       timer.
59
60   detect
61         my $class = Mojo::Reactor->detect;
62
63       Detect and load the best reactor implementation available, will try the
64       value of the "MOJO_REACTOR" environment variable, Mojo::Reactor::EV or
65       Mojo::Reactor::Poll.
66
67         # Instantiate best reactor implementation available
68         my $reactor = Mojo::Reactor->detect->new;
69
70   io
71         $reactor = $reactor->io($handle => sub {...});
72
73       Watch handle for I/O events, invoking the callback whenever handle
74       becomes readable or writable. Meant to be overloaded in a subclass.
75
76         # Callback will be executed twice if handle becomes readable and writable
77         $reactor->io($handle => sub {
78           my ($reactor, $writable) = @_;
79           say $writable ? 'Handle is writable' : 'Handle is readable';
80         });
81
82   is_running
83         my $bool = $reactor->is_running;
84
85       Check if reactor is running. Meant to be overloaded in a subclass.
86
87   next_tick
88         my $undef = $reactor->next_tick(sub {...});
89
90       Execute callback as soon as possible, but not before returning or other
91       callbacks that have been registered with this method, always returns
92       "undef". Meant to be overloaded in a subclass.
93
94   one_tick
95         $reactor->one_tick;
96
97       Run reactor until an event occurs. Note that this method can recurse
98       back into the reactor, so you need to be careful.  Meant to be
99       overloaded in a subclass.
100
101         # Don't block longer than 0.5 seconds
102         my $id = $reactor->timer(0.5 => sub {});
103         $reactor->one_tick;
104         $reactor->remove($id);
105
106   recurring
107         my $id = $reactor->recurring(0.25 => sub {...});
108
109       Create a new recurring timer, invoking the callback repeatedly after a
110       given amount of time in seconds. Meant to be overloaded in a subclass.
111
112   remove
113         my $bool = $reactor->remove($handle);
114         my $bool = $reactor->remove($id);
115
116       Remove handle or timer. Meant to be overloaded in a subclass.
117
118   reset
119         $reactor->reset;
120
121       Remove all handles and timers. Meant to be overloaded in a subclass.
122
123   start
124         $reactor->start;
125
126       Start watching for I/O and timer events, this will block until "stop"
127       is called. Note that some reactors stop automatically if there are no
128       events being watched anymore. Meant to be overloaded in a subclass.
129
130         # Start reactor only if it is not running already
131         $reactor->start unless $reactor->is_running;
132
133   stop
134         $reactor->stop;
135
136       Stop watching for I/O and timer events. Meant to be overloaded in a
137       subclass.
138
139   timer
140         my $id = $reactor->timer(0.5 => sub {...});
141
142       Create a new timer, invoking the callback after a given amount of time
143       in seconds. Meant to be overloaded in a subclass.
144
145   watch
146         $reactor = $reactor->watch($handle, $readable, $writable);
147
148       Change I/O events to watch handle for with true and false values. Meant
149       to be overloaded in a subclass. Note that this method requires an
150       active I/O watcher.
151
152         # Watch only for readable events
153         $reactor->watch($handle, 1, 0);
154
155         # Watch only for writable events
156         $reactor->watch($handle, 0, 1);
157
158         # Watch for readable and writable events
159         $reactor->watch($handle, 1, 1);
160
161         # Pause watching for events
162         $reactor->watch($handle, 0, 0);
163

SEE ALSO

165       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
166
167
168
169perl v5.32.0                      2020-07-28                  Mojo::Reactor(3)
Impressum