Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
log = logging.getLogger("cysystemd.async_reader")
class Base:
def __init__(self, loop: asyncio.AbstractEventLoop = None, executor=None):
self._executor = executor
self._loop = loop or asyncio.get_event_loop()
async def _exec(self, func, *args, **kwargs):
# noinspection PyTypeChecker
return await self._loop.run_in_executor(
self._executor, partial(func, *args, **kwargs)
)
class AsyncJournalReader(Base):
def __init__(self, executor=None, loop: asyncio.AbstractEventLoop = None):
super().__init__(loop=loop, executor=executor)
self.__reader = JournalReader()
self.__flags = None
self.__wait_lock = asyncio.Lock()
async def wait(self) -> bool:
async with self.__wait_lock:
loop = self._loop
reader = self.__reader
event = asyncio.Event()
loop.add_reader(reader.fd, event.set)
try:
await event.wait()
async def add_filter(self, rule):
return self._exec(self.__reader.add_filter, rule)
async def clear_filter(self):
return self._exec(self.__reader.clear_filter)
def __aiter__(self):
return AsyncReaderIterator(
loop=self._loop, executor=self._executor, reader=self.__reader
)
async def next(self, skip=0):
pass
class AsyncReaderIterator(Base, AsyncIterator):
QUEUE_SIZE = 1024
def __init__(self, *, reader, loop: asyncio.AbstractEventLoop, executor):
super().__init__(loop=loop, executor=executor)
self.reader = reader
self.queue = SimpleQueue()
self.event = asyncio.Event()
self.lock = asyncio.Lock()
self.closed = False
self._loop.create_task(self._exec(self._reader))
def _reader(self):
for item in self.reader:
self.queue.put(item)
self._loop.call_soon_threadsafe(self.event.set)