Skip to content

Commit ba068fd

Browse files
committed
Add asyncio example
1 parent d4bf97d commit ba068fd

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎doc/index.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ On Arch Linux and derivatives:
9191
9292
.. code-block:: bash
9393
94-
$ pacman -S core/linux-headers python-pip gcc
94+
$ pacman -S core/linux-api-headers python-pip gcc
9595
9696
Installing *python-evdev* with pip_:
9797

‎doc/tutorial.rst‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,42 @@ Reading events with asyncore
203203
InputEvent(1337255905L, 358857L, 0, 0, 0L)
204204

205205

206+
Reading events with asyncio
207+
===========================
208+
209+
An example program that prints the number of events by type every 5
210+
seconds:
211+
212+
::
213+
214+
import asyncio
215+
216+
from collections import Counter
217+
from functools import partial
218+
from evdev import InputDevice, ecodes
219+
220+
mouse = InputDevice('/dev/input/event1')
221+
keybd = InputDevice('/dev/input/event2')
222+
counter = Counter()
223+
224+
def read_events(device):
225+
events = device.read()
226+
counter.update([ev.type for ev in events])
227+
228+
@asyncio.coroutine
229+
def summarize():
230+
while True:
231+
yield from asyncio.sleep(5)
232+
print('Number of events by type in the last 5 seconds:')
233+
print({ecodes.EV[k]: v for k,v in counter.items()})
234+
counter.clear()
235+
236+
loop = asyncio.get_event_loop()
237+
loop.add_reader(mouse, partial(read_events, mouse))
238+
loop.add_reader(keybd, partial(read_events, keybd))
239+
loop.run_until_complete(summarize())
240+
241+
206242
Getting exclusive access to a device
207243
====================================
208244

0 commit comments

Comments
 (0)