1Mojo::Reactor::Poll(3)User Contributed Perl DocumentationMojo::Reactor::Poll(3)
2
3
4
6 Mojo::Reactor::Poll - Low-level event reactor with poll support
7
9 use Mojo::Reactor::Poll;
10
11 # Watch if handle becomes readable or writable
12 my $reactor = Mojo::Reactor::Poll->new;
13 $reactor->io($first => sub {
14 my ($reactor, $writable) = @_;
15 say $writable ? 'First handle is writable' : 'First handle is readable';
16 });
17
18 # Change to watching only if handle becomes writable
19 $reactor->watch($first, 0, 1);
20
21 # Turn file descriptor into handle and watch if it becomes readable
22 my $second = IO::Handle->new_from_fd($fd, 'r');
23 $reactor->io($second => sub {
24 my ($reactor, $writable) = @_;
25 say $writable ? 'Second handle is writable' : 'Second handle is readable';
26 })->watch($second, 1, 0);
27
28 # Add a timer
29 $reactor->timer(15 => sub {
30 my $reactor = shift;
31 $reactor->remove($first);
32 $reactor->remove($second);
33 say 'Timeout!';
34 });
35
36 # Start reactor if necessary
37 $reactor->start unless $reactor->is_running;
38
40 Mojo::Reactor::Poll is a low-level event reactor based on IO::Poll.
41
43 Mojo::Reactor::Poll inherits all events from Mojo::Reactor.
44
46 Mojo::Reactor::Poll inherits all methods from Mojo::Reactor and
47 implements the following new ones.
48
49 again
50 $reactor->again($id);
51 $reactor->again($id, 0.5);
52
53 Restart timer and optionally change the invocation time. Note that this
54 method requires an active timer.
55
56 io
57 $reactor = $reactor->io($handle => sub {...});
58
59 Watch handle for I/O events, invoking the callback whenever handle
60 becomes readable or writable.
61
62 # Callback will be executed twice if handle becomes readable and writable
63 $reactor->io($handle => sub {
64 my ($reactor, $writable) = @_;
65 say $writable ? 'Handle is writable' : 'Handle is readable';
66 });
67
68 is_running
69 my $bool = $reactor->is_running;
70
71 Check if reactor is running.
72
73 next_tick
74 my $undef = $reactor->next_tick(sub {...});
75
76 Execute callback as soon as possible, but not before returning or other
77 callbacks that have been registered with this method, always returns
78 "undef".
79
80 one_tick
81 $reactor->one_tick;
82
83 Run reactor until an event occurs or no events are being watched
84 anymore.
85
86 # Don't block longer than 0.5 seconds
87 my $id = $reactor->timer(0.5 => sub {});
88 $reactor->one_tick;
89 $reactor->remove($id);
90
91 recurring
92 my $id = $reactor->recurring(0.25 => sub {...});
93
94 Create a new recurring timer, invoking the callback repeatedly after a
95 given amount of time in seconds.
96
97 remove
98 my $bool = $reactor->remove($handle);
99 my $bool = $reactor->remove($id);
100
101 Remove handle or timer.
102
103 reset
104 $reactor->reset;
105
106 Remove all handles and timers.
107
108 start
109 $reactor->start;
110
111 Start watching for I/O and timer events, this will block until "stop"
112 is called or no events are being watched anymore.
113
114 # Start reactor only if it is not running already
115 $reactor->start unless $reactor->is_running;
116
117 stop
118 $reactor->stop;
119
120 Stop watching for I/O and timer events.
121
122 timer
123 my $id = $reactor->timer(0.5 => sub {...});
124
125 Create a new timer, invoking the callback after a given amount of time
126 in seconds.
127
128 watch
129 $reactor = $reactor->watch($handle, $readable, $writable);
130
131 Change I/O events to watch handle for with true and false values. Note
132 that this method requires an active I/O watcher.
133
134 # Watch only for readable events
135 $reactor->watch($handle, 1, 0);
136
137 # Watch only for writable events
138 $reactor->watch($handle, 0, 1);
139
140 # Watch for readable and writable events
141 $reactor->watch($handle, 1, 1);
142
143 # Pause watching for events
144 $reactor->watch($handle, 0, 0);
145
147 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
148
149
150
151perl v5.32.0 2020-07-28 Mojo::Reactor::Poll(3)