Last active
January 27, 2018 21:41
-
-
Save hiranya911/f2c5e2bcdf2fb3f3bd7d148e9d13f939 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 time | |
from firebase_admin import db | |
async def get_hero(key): | |
ref = db.reference('heroes').child(key) | |
# Calling a blocking method from a coroutine | |
return ref.get() | |
async def get_heroes_range(start, end): | |
coroutines = [get_hero('hero_{0}'.format(i)) 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)) | |
finally: | |
event_loop.close() | |
print('Time elapsed:', (time.time() - t0), 'seconds') | |
# Time elapsed: 18.832719087600708 seconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment