How does Kernel Maintain context of previous cells in a session

47 views
Skip to first unread message

zweb1

unread,
Jul 20, 2019, 10:08:29 PM7/20/19
to Project Jupyter
How does jupyter kernel maintain context of all the previous cell executions in a session? How is it implemented?

example:

cell [1]: import pandas

cell[2]: x = 7

cell[3] print(x)

3

When executing cell 3, Jupyter Kernel has x from cell 2 and pandas import from cell 1 execution. How is it implemented in the kernel?

Matthias Bussonnier

unread,
Jul 21, 2019, 12:31:41 AM7/21/19
to jup...@googlegroups.com
Hi Zweb1, 

THis is a really good question,

We use Python built-in exec (https://docs.python.org/3/library/functions.html#exec), which allow you to execute some code and pass a namespace in which to execute it. 
You then put it in a while loop, and wait for the user to send some code. Exec send the result back.
Here is a 5 lines REPL (read eval print loop) as  example on how to evaluate user code interactively.

$ cat repl.py

File: repl.py
   1   │
   2   │ namespace = dict()
   3   │
   4   │
   5   │ while True:
   6   │     code = input('>>> ')
   7   │     exec(code, namespace)

 $ python repl.py
>>> a = 1
>>> print(a)
1
>>> print(a+1)
2
>>>

You you were to look at namespace, you would see that after a = 1, we have 
namespace['a'] ==  1, so the namespace dict hold all our variables between exec.

This +/- 40 000 lines of code give you IPython, there is a lot not handled there, but if you take the above, handle exceptions, multiple statements.... etc and forward stdin and out over the network you have a rough prototype of what Jupyter is.

Does that answer your question? 
-- 
Matthias

--
You received this message because you are subscribed to the Google Groups "Project Jupyter" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jupyter+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jupyter/2312ef59-7f49-4ae4-927b-db278d69e35e%40googlegroups.com.

zweb1

unread,
Sep 11, 2019, 11:57:46 AM9/11/19
to Project Jupyter
Thanks Matthias, That is a very good explanation,

I assume namespace that gets passed around only stores the named reference to objects and not the data itself. 

Data and objects are in memory separately so it should be only limited by memory of server or is it limited by memory of python kernel?
To unsubscribe from this group and stop receiving emails from it, send an email to jup...@googlegroups.com.

Roland Weber

unread,
Sep 12, 2019, 1:32:30 AM9/12/19
to Project Jupyter
On Wednesday, September 11, 2019 at 5:57:46 PM UTC+2, zweb1 wrote:
I assume namespace that gets passed around only stores the named reference to objects and not the data itself. 

Data and objects are in memory separately so it should be only limited by memory of server or is it limited by memory of python kernel?

The namespace and all the data and objects it refers to exist in the heap of the process running the Python kernel.

cheers,
  Roland
Reply all
Reply to author
Forward
0 new messages