1Event(3) User Contributed Perl Documentation Event(3)
2
3
4
6 Coro::Event - do events the coro-way, with Event
7
9 use Coro;
10 use Coro::Event;
11
12 sub keyboard : Coro {
13 my $w = Coro::Event->io(fd => \*STDIN, poll => 'r');
14 while() {
15 print "cmd> ";
16 my $ev = $w->next; my $cmd = <STDIN>;
17 unloop unless $cmd ne "";
18 print "data> ";
19 my $ev = $w->next; my $data = <STDIN>;
20 }
21 }
22
23 loop;
24
25 # wait for input on stdin for one second
26 Coro::Event::do_io (fd => \*STDIN, timeout => 1) & Event::Watcher::R
27 or die "no input received";
28
29 # use a separate thread for event processing, if impossible in main:
30 Coro::async { Event::loop };
31
33 This module enables you to create programs using the powerful Event
34 model (and module), while retaining the linear style known from simple
35 or threaded programs.
36
37 This module provides a method and a function for every watcher type
38 (flavour) (see Event). The only difference between these and the
39 watcher constructors from Event is that you do not specify a callback
40 function - it will be managed by this module.
41
42 Your application should just create all necessary threads and then call
43 "Event::loop".
44
45 Please note that even programs or modules (such as Coro::Handle) that
46 use "traditional" event-based/continuation style will run more
47 efficient with this module then when using only Event.
48
50 Please note that Event does not support multithreading. That means that
51 you MUST NOT block in an event callback. Again: In Event callbacks, you
52 must never ever call a Coro function that blocks the current thread.
53
54 While this seems to work superficially, it will eventually cause memory
55 corruption and often results in deadlocks.
56
57 Best practise is to always use Coro::unblock_sub for your callbacks.
58
60 Whenever Event blocks (e.g. in a call to "one_event", "loop" etc.),
61 this module cede's to all other threads with the same or higher
62 priority. When any threads of lower priority are ready, it will not
63 block but run one of them and then check for events.
64
65 The effect is that coroutines with the same or higher priority than the
66 blocking coroutine will keep Event from checking for events, while
67 coroutines with lower priority are being run, but Event checks for new
68 events after every cede. Note that for this to work you actually need
69 to run the event loop in some thread.
70
72 $w = Coro::Event->flavour (args...)
73 Create and return a watcher of the given type.
74
75 Examples:
76
77 my $reader = Coro::Event->io (fd => $filehandle, poll => 'r');
78 $reader->next;
79
80 $w->next
81 Wait for and return the next event of the event queue of the
82 watcher. The returned event objects support two methods only:
83 "hits" and "got", both of which return integers: the number this
84 watcher was hit for this event, and the mask of poll events
85 received.
86
87 do_flavour args...
88 Create a watcher of the given type and immediately call its next
89 method, returning the event.
90
91 This is less efficient then calling the constructor once and the
92 next method often, but it does save typing sometimes.
93
94 sweep
95 Similar to Event::one_event and Event::sweep: The idle task is
96 called once (this has the effect of jumping back into the Event
97 loop once to serve new events).
98
99 The reason this function exists is that you sometimes want to serve
100 events while doing other work. Calling "Coro::cede" does not work
101 because "cede" implies that the current coroutine is runnable and
102 does not call into the Event dispatcher.
103
105 Marc A. Lehmann <schmorp@schmorp.de>
106 http://software.schmorp.de/pkg/Coro.html
107
108
109
110perl v5.32.0 2020-08-03 Event(3)