Skip to content

Instantly share code, notes, and snippets.

@nilp0inter
Created February 26, 2016 16:09
Show Gist options
  • Save nilp0inter/5f400b6afb6df7c0b553 to your computer and use it in GitHub Desktop.
Save nilp0inter/5f400b6afb6df7c0b553 to your computer and use it in GitHub Desktop.
Non blocking file reader for python asyncio
import asyncio
import sys
import os
LOOP = asyncio.get_event_loop()
class AsyncFile:
def __init__(self, filename):
self.filename = filename
async def __aenter__(self):
self._fileno = os.open(self.filename, os.O_NONBLOCK | os.O_RDONLY)
return self
async def __aexit__(self, type, value, traceback):
os.close(self._fileno)
async def __aiter__(self):
return self
async def __anext__(self):
result = None
is_readed = asyncio.Event()
def do_read(*_):
nonlocal result
result = os.read(self._fileno, 2147483647)
LOOP.remove_reader(self._fileno)
is_readed.set()
LOOP.add_reader(self._fileno, do_read)
await is_readed.wait()
if not result:
raise StopAsyncIteration()
else:
return result
async def print_all(filename):
le_finale = False
while not le_finale:
print("Starting reader for", filename)
async with AsyncFile(filename) as f:
async for chunk in f:
print(filename, chunk)
print("Finishing reader for", filename)
LOOP.run_until_complete(asyncio.wait([print_all(f) for f in sys.argv[1:]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment