Show More
@@ -89,6 +89,7 b' def _should_be_async(cell: str) -> bool:' | |||
|
89 | 89 | except SyntaxError: |
|
90 | 90 | try: |
|
91 | 91 | ast.parse(_asyncify(cell)) |
|
92 | # TODO verify ast has not "top level" return or yield. | |
|
92 | 93 | except SyntaxError: |
|
93 | 94 | return False |
|
94 | 95 | return True |
@@ -154,7 +154,7 b' def removed_co_newlocals(function:types.FunctionType) -> types.FunctionType:' | |||
|
154 | 154 | # async integration |
|
155 | 155 | from .async_helpers import (_asyncio_runner, _asyncify) |
|
156 | 156 | |
|
157 | if sys.version_info > (3,5): | |
|
157 | if sys.version_info > (3, 5): | |
|
158 | 158 | from .async_helpers import _curio_runner, _trio_runner, _should_be_async |
|
159 | 159 | else : |
|
160 | 160 | _curio_runner = _trio_runner = None |
@@ -8,10 +8,6 b' environment:' | |||
|
8 | 8 | PYTHON_VERSION: "3.6.x" |
|
9 | 9 | PYTHON_ARCH: "32" |
|
10 | 10 | |
|
11 | - PYTHON: "C:\\Python34-x64" | |
|
12 | PYTHON_VERSION: "3.4.x" | |
|
13 | PYTHON_ARCH: "64" | |
|
14 | ||
|
15 | 11 | - PYTHON: "C:\\Python36-x64" |
|
16 | 12 | PYTHON_VERSION: "3.6.x" |
|
17 | 13 | PYTHON_ARCH: "64" |
@@ -3,12 +3,17 b'' | |||
|
3 | 3 | Asynchronous in REPL: Autoawait |
|
4 | 4 | =============================== |
|
5 | 5 | |
|
6 |
Starting with IPython |
|
|
7 |
ability to run asynchronous code from the REPL. |
|
|
6 | Starting with IPython 7.0, and when user Python 3.6 and above, IPython offer the | |
|
7 | ability to run asynchronous code from the REPL. Constructs which are | |
|
8 | 8 | :exc:`SyntaxError` s in the Python REPL can be used seamlessly in IPython. |
|
9 | 9 | |
|
10 | When a supported libray is used, IPython will automatically `await` Futures | |
|
11 | and Coroutines in the REPL. This will happen if an :ref:`await <await>` (or `async`) is | |
|
10 | The example given here are for terminal IPython, running async code in a | |
|
11 | notebook interface or any other frontend using the Jupyter protocol will need to | |
|
12 | use a newer version of IPykernel. The details of how async code runs in | |
|
13 | IPykernel will differ between IPython, IPykernel and their versions. | |
|
14 | ||
|
15 | When a supported library is used, IPython will automatically `await` Futures | |
|
16 | and Coroutines in the REPL. This will happen if an :ref:`await <await>` is | |
|
12 | 17 | use at top level scope, or if any structure valid only in `async def |
|
13 | 18 | <https://docs.python.org/3/reference/compound_stmts.html#async-def>`_ function |
|
14 | 19 | context are present. For example, the following being a syntax error in the |
@@ -29,7 +34,7 b' Should behave as expected in the IPython REPL::' | |||
|
29 | 34 | |
|
30 | 35 | Python 3.6.0 |
|
31 | 36 | Type 'copyright', 'credits' or 'license' for more information |
|
32 |
IPython |
|
|
37 | IPython 7.0.0 -- An enhanced Interactive Python. Type '?' for help. | |
|
33 | 38 | |
|
34 | 39 | In [1]: import aiohttp |
|
35 | 40 | ...: result = aiohttp.get('https://api.github.com') |
@@ -58,7 +63,9 b' use the :magic:`%autoawait` magic to toggle the behavior at runtime::' | |||
|
58 | 63 | |
|
59 | 64 | By default IPython will assume integration with Python's provided |
|
60 | 65 | :mod:`asyncio`, but integration with other libraries is provided. In particular |
|
61 |
we provide experimental integration with the ``curio`` and ``trio`` library |
|
|
66 | we provide experimental integration with the ``curio`` and ``trio`` library, the | |
|
67 | later one being necessary if you require the ability to do nested call of | |
|
68 | IPython's ``embed()`` functionality. | |
|
62 | 69 | |
|
63 | 70 | You can switch current integration by using the |
|
64 | 71 | ``c.InteractiveShell.loop_runner`` option or the ``autoawait <name |
@@ -103,24 +110,25 b' other features of IPython and various registered extensions. In particular if yo' | |||
|
103 | 110 | are a direct or indirect user of the AST transformers, these may not apply to |
|
104 | 111 | your code. |
|
105 | 112 | |
|
106 | The default loop, or runner does not run in the background, so top level | |
|
107 | asynchronous code must finish for the REPL to allow you to enter more code. As | |
|
108 | with usual Python semantic, the awaitables are started only when awaited for the | |
|
109 | first time. That is to say, in first example, no network request is done between | |
|
110 | ``In[1]`` and ``In[2]``. | |
|
113 | When Using command line IPython, the default loop (or runner) does not process | |
|
114 | in the background, so top level asynchronous code must finish for the REPL to | |
|
115 | allow you to enter more code. As with usual Python semantic, the awaitables are | |
|
116 | started only when awaited for the first time. That is to say, in first example, | |
|
117 | no network request is done between ``In[1]`` and ``In[2]``. | |
|
111 | 118 | |
|
112 | 119 | |
|
113 | 120 | Internals |
|
114 | 121 | ========= |
|
115 | 122 | |
|
116 | As running asynchronous code is not supported in interactive REPL as of Python | |
|
117 |
3. |
|
|
118 |
It is interesting to understand how this works in order to |
|
|
123 | As running asynchronous code is not supported in interactive REPL (as of Python | |
|
124 | 3.7) we have to rely to a number of complex workaround to allow this to happen. | |
|
125 | It is interesting to understand how this works in order to comprehend potential | |
|
119 | 126 | bugs, or provide a custom runner. |
|
120 | 127 | |
|
121 | 128 | Among the many approaches that are at our disposition, we find only one that |
|
122 |
suited out need. Under the hood we |
|
|
123 |
and run it in global names |
|
|
129 | suited out need. Under the hood we use the code object from a async-def function | |
|
130 | and run it in global namespace after modifying it to not create a new | |
|
131 | ``locals()`` scope:: | |
|
124 | 132 | |
|
125 | 133 | async def inner_async(): |
|
126 | 134 | locals().update(**global_namespace) |
@@ -135,7 +143,7 b' and run it in global namesapace after modifying the ``__code__`` object.::' | |||
|
135 | 143 | |
|
136 | 144 | |
|
137 | 145 | The first thing you'll notice is that unlike classical ``exec``, there is only |
|
138 |
one name |
|
|
146 | one namespace. Second, user code runs in a function scope, and not a module | |
|
139 | 147 | scope. |
|
140 | 148 | |
|
141 | 149 | On top of the above there are significant modification to the AST of |
@@ -181,5 +189,5 b' We can set it up by passing it to ``%autoawait``::' | |||
|
181 | 189 | |
|
182 | 190 | |
|
183 | 191 | Asynchronous programming in python (and in particular in the REPL) is still a |
|
184 | relatively young subject. Feel free to contribute improvements to this codebase | |
|
185 | and give us feedback. | |
|
192 | relatively young subject. We expect some code to not behave as you expect, so | |
|
193 | feel free to contribute improvements to this codebase and give us feedback. |
@@ -35,6 +35,34 b' You should be able to update by using:' | |||
|
35 | 35 | pip install ipython --upgrade |
|
36 | 36 | |
|
37 | 37 | |
|
38 | Autowait: Asynchronous REPL | |
|
39 | =========================== | |
|
40 | ||
|
41 | Staring with IPython 7.0 and on Python 3.6+, IPython can automatically await | |
|
42 | code at top level, you should not need to access an event loop or runner | |
|
43 | yourself. To know more read the `autoawait`_ section of our docs, or try the | |
|
44 | following code:: | |
|
45 | ||
|
46 | In [6]: from asyncio import sleep | |
|
47 | ...: print('Going to sleep...') | |
|
48 | ...: await sleep(3) | |
|
49 | ...: print('Waking up') | |
|
50 | Going to sleep... | |
|
51 | Waking up | |
|
52 | ||
|
53 | Asynchronous code in a Notebook interface or any other frontend using the | |
|
54 | Jupyter Protocol will need further updates of the IPykernel package. | |
|
55 | ||
|
56 | ||
|
57 | Change to Nested Embed | |
|
58 | ====================== | |
|
59 | ||
|
60 | The introduction of the ability to run async code had ripple effect on the | |
|
61 | ability to use nested IPython. You may need to install the ``trio`` library | |
|
62 | (version 05 at the time of this writing) to | |
|
63 | have this feature working. | |
|
64 | ||
|
65 | ||
|
38 | 66 | .. DO NOT EDIT THIS LINE BEFORE RELEASE. FEATURE INSERTION POINT. |
|
39 | 67 | |
|
40 | 68 |
General Comments 0
You need to be logged in to leave comments.
Login now