Created
January 16, 2018 11:47
-
-
Save arjunv/e72bb10051eb588dcadadafac578d801 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import aiofiles | |
async def reader(): | |
async with aiofiles.open('file.txt', mode='r') as name_file: | |
while True: | |
yield await name_file.readline() | |
async def fetcher(reader_gen, i): | |
print('Opening {}'.format(i)) | |
while(True): | |
name = await reader_gen.__anext__() | |
if name.strip(): | |
# line has some value | |
print('Got name "{}" for {}'.format(name.strip(), i)) | |
elif name: | |
# implies it contains only '\n', ie empty line | |
print('Got an empty line for {}'.format(i)) | |
pass | |
else: | |
# reaching here is EOF | |
print('No more names. Closing {}'.format(i)) | |
break | |
# await asyncio.sleep(0) | |
# uncommenting above line gives the intended output, implying that aiofiles fails to async the file io call, right? | |
looper = asyncio.get_event_loop() | |
tasks = [] | |
reader_gen = reader() | |
for i in range(5): | |
tasks.append(asyncio.ensure_future(fetcher(reader_gen, i))) | |
looper.run_until_complete(asyncio.wait(tasks)) | |
reader_gen.aclose() | |
# # file.txt | |
# name1 | |
# name2 | |
# name3 | |
# | |
# name4 | |
# name5 | |
# name6 | |
# # output | |
# Opening 0 | |
# Opening 1 | |
# Opening 2 | |
# Opening 3 | |
# Opening 4 | |
# Got name "name1" for 0 | |
# Got name "name2" for 0 | |
# Got name "name3" for 0 | |
# Got an empty line for 0 | |
# Got name "name4" for 0 | |
# Got name "name5" for 0 | |
# Got name "name6" for 0 | |
# No more names. Closing 0 | |
# No more names. Closing 1 | |
# No more names. Closing 2 | |
# No more names. Closing 3 | |
# No more names. Closing 4 | |
# # expected output | |
# Opening 0 | |
# Opening 1 | |
# Opening 2 | |
# Opening 3 | |
# Opening 4 | |
# Got name "name1" for 0 | |
# Got name "name2" for 1 | |
# Got name "name3" for 2 | |
# Got an empty line for 3 | |
# Got name "name4" for 4 | |
# Got name "name5" for 0 | |
# Got name "name6" for 1 | |
# No more names. Closing 0 | |
# No more names. Closing 1 | |
# No more names. Closing 2 | |
# No more names. Closing 3 | |
# No more names. Closing 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment