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