Skip to content

Instantly share code, notes, and snippets.

@sergeant-wizard
Created November 4, 2018 14:01
Show Gist options
  • Save sergeant-wizard/fedd4a2058ddda56fd3821b070665a1e to your computer and use it in GitHub Desktop.
Save sergeant-wizard/fedd4a2058ddda56fd3821b070665a1e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"import time"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# If the method is a coroutine"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<_GatheringFuture pending>"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"start hoge...\n",
"start foo...\n",
"...end hoge\n",
"...end foo\n"
]
}
],
"source": [
"async def hoge():\n",
" print('start hoge...')\n",
" await asyncio.sleep(1)\n",
" print('...end hoge')\n",
"\n",
"async def foo():\n",
" print('start foo...')\n",
" await asyncio.sleep(1)\n",
" print('...end foo')\n",
"\n",
"asyncio.gather(hoge(), foo())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# sync with Task"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"start foo...\n",
"start hoge...\n",
"...end foo\n",
"...end hoge\n"
]
}
],
"source": [
"async def hoge():\n",
" print('start hoge...')\n",
" await asyncio.sleep(1)\n",
" print('...end hoge')\n",
"\n",
"def foo():\n",
" print('start foo...')\n",
" time.sleep(1)\n",
" print('...end foo')\n",
"\n",
"loop = asyncio.get_event_loop()\n",
"hoge_task = asyncio.Task(hoge())\n",
"await loop.run_in_executor(None, foo) # simply calling foo() won't work (???)\n",
"await hoge_task"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# If the library supports futures"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"start hoge...\n",
"start foo...\n",
"...end hoge\n",
"...end foo\n",
"hoge result\n"
]
}
],
"source": [
"def hoge(): # returns future\n",
" def execute():\n",
" print('start hoge...')\n",
" time.sleep(1)\n",
" print('...end hoge')\n",
" return('hoge result')\n",
" loop = asyncio.get_event_loop()\n",
" return loop.run_in_executor(None, execute)\n",
"\n",
"def foo():\n",
" print('start foo...')\n",
" time.sleep(1)\n",
" print('...end foo')\n",
"\n",
"\n",
"hoge_future = hoge()\n",
"foo() # blocks\n",
"hoge_result = await hoge_future\n",
"print(hoge_result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# If the method is simply blocking"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"start hoge...\n",
"start foo...\n",
"...end hoge\n",
"...end foo\n"
]
}
],
"source": [
"def hoge():\n",
" print('start hoge...')\n",
" time.sleep(1)\n",
" print('...end hoge')\n",
"\n",
"def foo():\n",
" print('start foo...')\n",
" time.sleep(1)\n",
" print('...end foo')\n",
" \n",
"loop = asyncio.get_event_loop()\n",
"hoge_task = loop.run_in_executor(None, hoge)\n",
"foo() # blocks\n",
"await hoge_task"
]
}
],
"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.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment