Skip to content

Instantly share code, notes, and snippets.

@hiranya911
Created January 27, 2018 21:42
Show Gist options
  • Save hiranya911/a0333aaef2d5165448bd73aa9c158ccd to your computer and use it in GitHub Desktop.
Save hiranya911/a0333aaef2d5165448bd73aa9c158ccd to your computer and use it in GitHub Desktop.
import asyncio
import concurrent.futures
import time
from firebase_admin import db
executor = concurrent.futures.ThreadPoolExecutor(max_workers=20)
async def get_hero(key, event_loop):
ref = db.reference('heroes').child(key)
# Blocking method is delegated to the thread pool
return await event_loop.run_in_executor(executor, ref.get)
async def get_heroes_range(start, end, event_loop):
coroutines = [get_hero('hero_{0}'.format(i), event_loop) for i in range(start, end)]
completed, pending = await asyncio.wait(coroutines)
for item in completed:
print(item.result())
t0 = time.time()
event_loop = asyncio.new_event_loop()
try:
event_loop.run_until_complete(get_heroes_range(0, 100, event_loop))
finally:
event_loop.close()
print('Time elapsed:', (time.time() - t0), 'seconds')
# Time elapsed: 1.6605749130249023 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment