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