##// END OF EJS Templates
Prototype async REPL using IPython, take III...
Prototype async REPL using IPython, take III This is a squash and a rebase of a large number of commits from Min and I. For simplicity of managing it, history has been reduced to a single commit, but more historical versions can be found, in particular in PR 11155, or commit aedb5d6d3a441dcdb7180ac9b5cc03f91329117b to be more exact.

File last commit:

r24463:6f33fcd4
r24463:6f33fcd4
Show More
await-repl.rst
55 lines | 1.5 KiB | text/x-rst | RstLexer

Await REPL

:ghpull:`10390` introduced the ability to await Futures and Coroutines in the REPL. For example:

Python 3.6.0
Type 'copyright', 'credits' or 'license' for more information
IPython 6.0.0.dev -- An enhanced Interactive Python. Type '?' for help.

In [1]: import aiohttp
   ...: result = aiohttp.get('https://api.github.com')

In [2]: response = await result
<pause for a few 100s ms>

In [3]: await response.json()
Out[3]:
{'authorizations_url': 'https://api.github.com/authorizations',
 'code_search_url': 'https://api.github.com/search/code?q={query}{&page,per_page,sort,order}',
...
}

Integration is by default with asyncio, but other libraries can be configured, like curio or trio, to improve concurrency in the REPL:

In [1]: %autoawait trio

In [2]: import trio

In [3]: async def child(i):
   ...:     print("   child %s goes to sleep"%i)
   ...:     await trio.sleep(2)
   ...:     print("   child %s wakes up"%i)

In [4]: print('parent start')
   ...: async with trio.open_nursery() as n:
   ...:     for i in range(3):
   ...:         n.spawn(child, i)
   ...: print('parent end')
parent start
   child 2 goes to sleep
   child 0 goes to sleep
   child 1 goes to sleep
   <about 2 seconds pause>
   child 2 wakes up
   child 1 wakes up
   child 0 wakes up
parent end

See :ref:`autoawait` for more information.