Skip to content

Instantly share code, notes, and snippets.

@jbcurtin
Last active August 5, 2018 00:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbcurtin/9ce51982cbc76bd43e72f08847c288a3 to your computer and use it in GitHub Desktop.
Save jbcurtin/9ce51982cbc76bd43e72f08847c288a3 to your computer and use it in GitHub Desktop.
from IPython.core.magic import Magics, magics_class, line_magic
import asyncio
from concurrent.futures import Future
# Credits goes to https://gist.github.com/chrisseto/8f7289497f40ad442a30#gistcomment-1701177
@magics_class
class AsyncMagics(Magics):
@line_magic
def await(self, line):
value = eval(line, self.shell.user_global_ns, self.shell.user_ns)
if isinstance(value, Future):
value = asyncio.wrap_future(value)
return asyncio.get_event_loop().run_until_complete(value)
def load_ipython_extension(ipython):
ipython.register_magics(AsyncMagics)
# Usage
# >>> %install_ext https://gist.githubusercontent.com/jbcurtin/9ce51982cbc76bd43e72f08847c288a3/raw/e85873b33006e4ac7fba3f4b1464fb9124b0a91d/await.py
# Installed await.py. To use it, type:
# %load_ext await
# >>> %load_ext await
# >>> import aiohttp
# >>> %await aiohttp.request('GET', 'http://google.com')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment