Skip to content

Commit 27dc2c3

Browse files
committed
Support for new asyncio coroutines
The new asyncio API is very powerful, and much easier than working with selectors directly. This patch adds the coroutines `async_read` and `async_read_one`, and makes `read_loop()` compatible with the new `async for` syntax. Unfortunately, the code turned out a bit ugly in order to make the syntax backward compatible with Python 2.7.
1 parent 14fc704 commit 27dc2c3

1 file changed

Lines changed: 76 additions & 4 deletions

File tree

‎evdev/eventio.py‎

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
from evdev import _input, _uinput, ecodes, util
1010
from evdev.events import InputEvent
1111

12+
try:
13+
import asyncio
14+
except ImportError:
15+
asyncio = None
16+
1217
class EventIO(object):
1318
'''
1419
Class capable of read and write input events.
@@ -31,11 +36,78 @@ def fileno(self):
3136
def read_loop(self):
3237
'''
3338
Enter an endless loop that yields input events.
39+
40+
The returned iterator is compatible with `async for` syntax
3441
'''
35-
while True:
36-
r, w, x = select.select([self.fd], [], [])
37-
for event in self.read():
38-
yield event
42+
class ReadIterator:
43+
def __init__(self, device):
44+
self.current_batch = iter(())
45+
self.device = device
46+
47+
# Standard iterator
48+
def __iter__(self):
49+
return self
50+
51+
def next(self): # Python 2.x
52+
return self.__next__()
53+
54+
def __next__(self): # Python 3.x
55+
try:
56+
# Try to read from previous batch
57+
return next(self.current_batch)
58+
except StopIteration:
59+
# Fetch next batch
60+
r, w, x = select.select([self.device.fd], [], [])
61+
self.current_batch = self.device.read()
62+
return next(self.current_batch)
63+
64+
# Async iteration - Python 3.5
65+
if asyncio is not None:
66+
@asyncio.coroutine
67+
def __aiter__(self):
68+
return self
69+
70+
@asyncio.coroutine
71+
def __anext__(self):
72+
future = asyncio.Future()
73+
try:
74+
# Try to read from previous batch
75+
future.set_result(next(self.current_batch))
76+
except StopIteration:
77+
# Fetch next batch
78+
def next_batch_ready(batch):
79+
self.current_batch = batch.result()
80+
future.set_result(next(self.current_batch))
81+
self.device.async_read().add_done_callback(next_batch_ready)
82+
return future
83+
84+
return ReadIterator(self)
85+
86+
if asyncio is not None:
87+
def _do_when_readable(self, callback):
88+
loop = asyncio.get_event_loop()
89+
def ready():
90+
loop.remove_reader(self.fileno())
91+
callback()
92+
loop.add_reader(self.fileno(), ready)
93+
94+
def async_read_one(self):
95+
'''
96+
asyncio coroutine to read and return a single input event as an instance of
97+
:class:`InputEvent <evdev.events.InputEvent>`.
98+
'''
99+
future = asyncio.Future()
100+
self._do_when_readable(lambda: future.set_result(self.read_one()))
101+
return future
102+
103+
def async_read(self):
104+
'''
105+
asyncio coroutine to read multiple input events from device. Return a generator object that
106+
yields :class:`InputEvent <evdev.events.InputEvent>` instances.
107+
'''
108+
future = asyncio.Future()
109+
self._do_when_readable(lambda: future.set_result(self.read()))
110+
return future
39111

40112

41113
def read_one(self):

0 commit comments

Comments
 (0)