Skip to content

Instantly share code, notes, and snippets.

@im-noob
Last active September 5, 2020 13:16
Show Gist options
  • Save im-noob/7699a2b0ec0ef65ea3ce53a7781b6ac6 to your computer and use it in GitHub Desktop.
Save im-noob/7699a2b0ec0ef65ea3ce53a7781b6ac6 to your computer and use it in GitHub Desktop.
asyncio python Example
import asyncio
import nest_asyncio
nest_asyncio.apply()
async def task(batch_no,roll_no):
return batch_no,roll_no
async def batch_creator(batch_no):
result = []
for roll_no in range(BATCH_SIZE):
result.append(loop.create_task(task(batch_no,roll_no)))
return result
async def resCollector():
for batch_no in range(BATCH_COUNT):
for m in await batch_creator(batch_no):
output = await m
with open('output.txt','a') as f:
f.write(str(output)+'\n')
loop = asyncio.get_event_loop()
final = asyncio.run(resCollector())
print('Finish Execution')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment