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
52 Restart timer. Note that this method requires an active timer.
53
54 io
55 $reactor = $reactor->io($handle => sub {...});
56
57 Watch handle for I/O events, invoking the callback whenever handle
58 becomes readable or writable.
59
60 # Callback will be executed twice if handle becomes readable and writable
61 $reactor->io($handle => sub {
62 my ($reactor, $writable) = @_;
63 say $writable ? 'Handle is writable' : 'Handle is readable';
64 });
65
66 is_running
67 my $bool = $reactor->is_running;
68
69 Check if reactor is running.
70
71 next_tick
72 my $undef = $reactor->next_tick(sub {...});
73
74 Execute callback as soon as possible, but not before returning or other
75 callbacks that have been registered with this method, always returns
76 "undef".
77
78 one_tick
79 $reactor->one_tick;
80
81 Run reactor until an event occurs or no events are being watched
82 anymore.
83
84 # Don't block longer than 0.5 seconds
85 my $id = $reactor->timer(0.5 => sub {});
86 $reactor->one_tick;
87 $reactor->remove($id);
88
89 recurring
90 my $id = $reactor->recurring(0.25 => sub {...});
91
92 Create a new recurring timer, invoking the callback repeatedly after a
93 given amount of time in seconds.
94
95 remove
96 my $bool = $reactor->remove($handle);
97 my $bool = $reactor->remove($id);
98
99 Remove handle or timer.
100
101 reset
102 $reactor->reset;
103
104 Remove all handles and timers.
105
106 start
107 $reactor->start;
108
109 Start watching for I/O and timer events, this will block until "stop"
110 is called or no events are being watched anymore.
111
112 # Start reactor only if it is not running already
113 $reactor->start unless $reactor->is_running;
114
115 stop
116 $reactor->stop;
117
118 Stop watching for I/O and timer events.
119
120 timer
121 my $id = $reactor->timer(0.5 => sub {...});
122
123 Create a new timer, invoking the callback after a given amount of time
124 in seconds.
125
126 watch
127 $reactor = $reactor->watch($handle, $readable, $writable);
128
129 Change I/O events to watch handle for with true and false values. Note
130 that this method requires an active I/O watcher.
131
132 # Watch only for readable events
133 $reactor->watch($handle, 1, 0);
134
135 # Watch only for writable events
136 $reactor->watch($handle, 0, 1);
137
138 # Watch for readable and writable events
139 $reactor->watch($handle, 1, 1);
140
141 # Pause watching for events
142 $reactor->watch($handle, 0, 0);
143
145 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
146
147
148
149perl v5.30.0 2019-07-26 Mojo::Reactor::Poll(3)