async_client.py
Go to the documentation of this file.
''' Asynchronous python bindings for the lircd socket interface. '''
# pylint: disable=W0613
import asyncio
from lirc.client import AbstractConnection as AbstractConnection
class AsyncConnection(object):
''' Asynchronous read interface on top of an AbstractConnection.
Parameters:
- connection: Typically a lirc.RawConnection or lirc.LircdConnection.
- loop: AbstractEventLoop, typically obtained using
asyncio.get_event_loop().
'''
def __init__(self, connection: AbstractConnection,
loop: asyncio.AbstractEventLoop):
def read_from_fd():
''' Read data from the connection fd and put into queue. '''
line = self._conn.readline(0)
if line:
asyncio.ensure_future(self._queue.put(line))
self._conn = connection
self._loop = loop
self._queue = asyncio.Queue(loop=self._loop)
self._loop.add_reader(self._conn.fileno(), read_from_fd)
def close(self):
''' Clean up loop and the base connection. '''
self._loop.remove_reader(self._conn.fileno())
async def readline(self) -> str:
''' Asynchronous get next line from the connection. '''
return await self._queue.get()
def __aiter__(self):
''' Return async iterator. '''
return self
async def __anext__(self):
''' Implement async iterator.next(). '''
return await self._queue.get()
async def __aenter__(self):
''' Implement "async with". '''
return self
async def __aexit__(self, exc_type, exc, traceback):
''' Implement exit from "async with". '''
self.close()
lirc.async_client.AsyncConnection.readline
def readline(self)
Definition: async_client.py:73
lirc.async_client.AsyncConnection.__aenter__
def __aenter__(self)
Definition: async_client.py:85
lirc.async_client.AsyncConnection._loop
_loop
Definition: async_client.py:65
lirc.async_client.AsyncConnection._queue
_queue
Definition: async_client.py:66
lirc.async_client.AsyncConnection.close
def close(self)
Clean up loop and the base connection.
Definition: async_client.py:70
lirc.async_client.AsyncConnection._conn
_conn
Read data from the connection fd and put into queue.
Definition: async_client.py:64
lirc.async_client.AsyncConnection.__anext__
def __anext__(self)
Definition: async_client.py:81
lirc.async_client.AsyncConnection.__aiter__
def __aiter__(self)
Return async iterator.
Definition: async_client.py:78
lirc.async_client.AsyncConnection.__aexit__
def __aexit__(self, exc_type, exc, traceback)
Definition: async_client.py:89
lirc.client
Definition: client.py:1