I'm trying to replace list of generated numbers with asynchronously reading them from file. How can I do it right?
from asyncio import get_event_loop, gather, sleep
import aiofiles
async def read_file():
async with aiofiles.open('test.txt', mode='rb') as f:
async for line in f:
yield line
async def main(k):
print(k)
await sleep(1)
if __name__ == '__main__':
count_group = 3
list_objects = list()
for i in range(1, 11):
list_objects.append(i)
loop = get_event_loop()
# TODO How to correctly replace list_objects with read_file()
list_func = [main(x) for x in list_objects]
run_groups = [list_func[i:i + count_group] for i in range(0, len(list_func), count_group)]
for rg in run_groups:
loop.run_until_complete(gather(*rg))
I tried different options but none of them work. My goal is to asynchronously read lines from file and work with them.
You could do the following: