Created
January 27, 2018 21:42
-
-
Save hiranya911/a0333aaef2d5165448bd73aa9c158ccd 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 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