##// END OF EJS Templates
Expand docs on how cells are executed
anatoly techtonik -
Show More
@@ -1,13 +1,14 b''
1 1 .. _execution_semantics:
2 2
3 Execution semantics in the IPython kernel
4 =========================================
3 Execution of cells in the IPython kernel
4 ========================================
5 5
6 The execution of user code consists of the following phases:
6 When IPython kernel receives `execute_request <https://jupyter-client.readthedocs.io/en/latest/messaging.html#execute>`_
7 with user code, it processes the message in the following phases:
7 8
8 9 1. Fire the ``pre_execute`` event.
9 10 2. Fire the ``pre_run_cell`` event unless silent is ``True``.
10 3. Execute the ``code`` field, see below for details.
11 3. Execute ``run_cell`` method to preprocess ``code``, compile and run it, see below for details.
11 12 4. If execution succeeds, expressions in ``user_expressions`` are computed.
12 13 This ensures that any error in the expressions don't affect the main code execution.
13 14 5. Fire the ``post_execute`` event.
@@ -18,9 +19,15 b' The execution of user code consists of the following phases:'
18 19 :doc:`/config/callbacks`
19 20
20 21
21 To understand how the ``code`` field is executed, one must know that Python
22 code can be compiled in one of three modes (controlled by the ``mode`` argument
23 to the :func:`compile` builtin):
22 Running user ``code``
23 =====================
24
25 First, the ``code`` cell is transformed to expand ``%magic`` and ``!system``
26 by ``IPython.core.inputtransformer2``. Then is being compiled using standard
27 Python :func:`compile` function and executed.
28
29 Not specific to IPython, all Python code can be compiled in one of three modes
30 (see ``mode`` argument to the standard :func:`compile` function):
24 31
25 32 *single*
26 33 Valid for a single interactive statement (though the source can contain
@@ -50,16 +57,16 b" execution in 'single' mode, and then:"
50 57
51 58 - If there is more than one block:
52 59
53 * if the last one is a single line long, run all but the last in 'exec' mode
60 * if the last block is a single line long, run all but the last in 'exec' mode
54 61 and the very last one in 'single' mode. This makes it easy to type simple
55 62 expressions at the end to see computed values.
56 63
57 * if the last one is no more than two lines long, run all but the last in
64 * if the last block is no more than two lines long, run all but the last in
58 65 'exec' mode and the very last one in 'single' mode. This makes it easy to
59 66 type simple expressions at the end to see computed values. - otherwise
60 67 (last one is also multiline), run all in 'exec' mode
61 68
62 * otherwise (last one is also multiline), run all in 'exec' mode as a single
69 * otherwise (last block is also multiline), run all in 'exec' mode as a single
63 70 unit.
64 71
65 72
General Comments 0
You need to be logged in to leave comments. Login now