Created
December 25, 2016 23:05
-
-
Save jbaiter/c83e68259d257f78af7efb4ee5dad0c7 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 threading import Thread | |
import aiohttp | |
import janus | |
# q: do i really have to pass the loop from the main thread around? | |
async def fetch(url, loop): | |
async with aiohttp.ClientSession(loop=loop) as session: | |
async with session.get(url) as response: | |
return await response.read() | |
async def fetch_many(loop, q): | |
url = "http://localhost:8000/{}" | |
tasks = [] | |
for i in range(10): | |
task = asyncio.ensure_future(fetch(url.format(i), loop), | |
loop=loop) | |
tasks.append(task) | |
for f in asyncio.as_completed(tasks, loop=loop): | |
await q.put(await f) | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
# q: do i really need janus to share a queue between sync and async code? | |
queue = janus.Queue(loop=loop) | |
thread = Thread(target=loop.run_until_complete, | |
args=(fetch_many(loop, queue.async_q),)) | |
thread.start() | |
while thread.is_alive() or not queue.sync_q.empty(): | |
result = queue.sync_q.get() | |
if result is None: | |
break | |
print(result) | |
queue.sync_q.task_done() | |
thread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment