Skip to content

Instantly share code, notes, and snippets.

@luxcem
Last active December 22, 2017 16:23
Show Gist options
  • Save luxcem/a97a696a9d9520fb62ca961651ab0235 to your computer and use it in GitHub Desktop.
Save luxcem/a97a696a9d9520fb62ca961651ab0235 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python asyncio TL;DR"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We need asyncio and aihttp, asyncio is the python implementation of coroutines, tasks and event loops.\n",
"aiohttp is the implementation of network async requests, think of it as the async equivalent of requests."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"import aiohttp\n",
"import json"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An event loop essentially manages and distributes the execution of different tasks. It registers them and handles distributing the flow of control between them."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"loop = asyncio.get_event_loop()\n",
"client = aiohttp.ClientSession(loop=loop)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Coroutine that handle the download of urls\n",
"async def get_json(url): \n",
" async with client.get(url) as response:\n",
" assert response.status == 200\n",
" data = await response.read()\n",
" return json.loads(data)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"urls = [\n",
" \"https://www.reddit.com/r/Python/top/.json?sort=top&t=all&count=20\",\n",
" \"https://www.reddit.com/r/dataisbeautiful/top/.json?sort=top&t=all&count=20\",\n",
" \"https://www.reddit.com/r/MachineLearning/top/.json?sort=top&t=all&count=20\"\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"tasks = [\n",
" asyncio.ensure_future(get_json(url))\n",
" for url in urls\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<Task pending coro=<get_json() running at <ipython-input-3-905d3c68b808>:2>>,\n",
" <Task pending coro=<get_json() running at <ipython-input-3-905d3c68b808>:2>>,\n",
" <Task pending coro=<get_json() running at <ipython-input-3-905d3c68b808>:2>>]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tasks"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"final = loop.run_until_complete(asyncio.gather(*tasks))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_keys(['modhash', 'whitelist_status', 'children', 'after', 'before'])\n",
"dict_keys(['modhash', 'whitelist_status', 'children', 'after', 'before'])\n",
"dict_keys(['modhash', 'whitelist_status', 'children', 'after', 'before'])\n"
]
}
],
"source": [
"for i in final:\n",
" print(i[\"data\"].keys())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment