1Mojo::Reactor::EV(3) User Contributed Perl Documentation Mojo::Reactor::EV(3)
2
3
4
6 Mojo::Reactor::EV - Low-level event reactor with libev support
7
9 use Mojo::Reactor::EV;
10
11 # Watch if handle becomes readable or writable
12 my $reactor = Mojo::Reactor::EV->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::EV is a low-level event reactor based on EV (4.32+).
38
40 Mojo::Reactor::EV inherits all events from Mojo::Reactor::Poll.
41
43 Mojo::Reactor::EV inherits all methods from Mojo::Reactor::Poll 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 new
54 my $reactor = Mojo::Reactor::EV->new;
55
56 Construct a new Mojo::Reactor::EV object.
57
58 one_tick
59 $reactor->one_tick;
60
61 Run reactor until an event occurs or no events are being watched
62 anymore.
63
64 # Don't block longer than 0.5 seconds
65 my $id = $reactor->timer(0.5 => sub {});
66 $reactor->one_tick;
67 $reactor->remove($id);
68
69 recurring
70 my $id = $reactor->recurring(0.25 => sub {...});
71
72 Create a new recurring timer, invoking the callback repeatedly after a
73 given amount of time in seconds.
74
75 start
76 $reactor->start;
77
78 Start watching for I/O and timer events, this will block until "stop"
79 is called or no events are being watched anymore.
80
81 # Start reactor only if it is not running already
82 $reactor->start unless $reactor->is_running;
83
84 stop
85 $reactor->stop;
86
87 Stop watching for I/O and timer events.
88
89 timer
90 my $id = $reactor->timer(0.5 => sub {...});
91
92 Create a new timer, invoking the callback after a given amount of time
93 in seconds.
94
95 watch
96 $reactor = $reactor->watch($handle, $readable, $writable);
97
98 Change I/O events to watch handle for with true and false values. Note
99 that this method requires an active I/O watcher.
100
101 # Watch only for readable events
102 $reactor->watch($handle, 1, 0);
103
104 # Watch only for writable events
105 $reactor->watch($handle, 0, 1);
106
107 # Watch for readable and writable events
108 $reactor->watch($handle, 1, 1);
109
110 # Pause watching for events
111 $reactor->watch($handle, 0, 0);
112
114 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
115
116
117
118perl v5.32.1 2021-02-07 Mojo::Reactor::EV(3)