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 ($reactor, $err) {...});
35
36       Emitted for exceptions caught in callbacks, fatal if unhandled. Note
37       that if this event is unhandled or fails it might kill your program, so
38       you need to be careful.
39
40         $reactor->on(error => sub ($reactor, $err) { say "Something very bad happened: $err" });
41

METHODS

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

SEE ALSO

158       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
159
160
161
162perl v5.38.0                      2023-09-11                  Mojo::Reactor(3)
Impressum