Skip to content

Commit

Permalink
Improve the asyncio examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gvalkov committed Feb 7, 2016
1 parent 936a1ca commit 96629bc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -249,7 +249,7 @@
'Miscellaneous'),
]

intersphinx_mapping = {'python': ('http://docs.python.org/3.5', None)}
intersphinx_mapping = {'python': ('http://docs.python.org/3', None)}

# Documents to append as an appendix to all manuals.
#texinfo_appendices = []
Expand Down
59 changes: 38 additions & 21 deletions docs/tutorial.rst
@@ -1,6 +1,7 @@
Tutorial
--------


Listing accessible event devices
================================

Expand Down Expand Up @@ -158,33 +159,49 @@ This can also be achieved using the selectors_ module in Python 3.4:
for event in device.read():
print(event)

Yet another possibility is asyncio in Python 3.4:

Yet another possibility is the :mod:`asyncio` module from Python 3.4:

::

import asyncio
from evdev import list_devices, InputDevice, categorize
import asyncio, evdev

@asyncio.coroutine
def print_events(device):
while True:
events = yield from device.async_read()
for event in events:
print(device.fn, evdev.categorize(event), sep=': ')

mouse = evdev.InputDevice('/dev/input/eventX')
keybd = evdev.InputDevice('/dev/input/eventY')

for device in mouse, keybd:
asyncio.async(print_events(device))

# Python 3.5 syntax, using "async for"
async def dump_events(device):
async for ev in device.read_loop():
print(device.name, categorize(ev))
loop = asyncio.get_event_loop()
loop.run_forever()

## Python 3.4 syntax, using "yield from" in an infinite loop
#@asyncio.coroutine
#def dump_events(device):
# while True:
# evs = yield from device.async_read()
# for ev in evs:
# print(device.name, categorize(ev))
Since Python 3.5, the `async/await
<https://docs.python.org/3/library/asyncio-task.html>`_ syntax makes this
even simpler:

for file in list_devices():
device = InputDevice(file)
asyncio.ensure_future( dump_events( device ) )
::

import asyncio, evdev

mouse = evdev.InputDevice('/dev/input/event4')
keybd = evdev.InputDevice('/dev/input/event5')

asyncio.get_event_loop().run_forever()
async def print_events(device):
async for event in device.read_iter():
print(device.fn, evdev.categorize(event), sep=': ')

for device in mouse, keybd:
asyncio.ensure_future(print_events(device))

loop = asyncio.get_event_loop()
loop.run_forever()


Accessing evdev constants
Expand Down Expand Up @@ -238,7 +255,7 @@ To read a single event, you can use device.async_read_one()

::

event = await dev.async_read_one()
event = await dev.async_read_one()

To read a batch of events, you can use device.async_read():

Expand All @@ -253,7 +270,7 @@ To read all events in an infinite loop, use dev.read_loop() and `async for`:
::
async for ev in dev.read_loop():
print(categorize(ev))

See "Reading events from multiple devices" for a complete example using asyncio.


Expand Down

0 comments on commit 96629bc

Please sign in to comment.