Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reuse exsisting kernel? #238

Closed
pplonski opened this issue Jun 29, 2022 · 4 comments · Fixed by #239
Closed

How to reuse exsisting kernel? #238

pplonski opened this issue Jun 29, 2022 · 4 comments · Fixed by #239

Comments

@pplonski
Copy link

Hi!

I would like to create a kernel that can be reused between executions. For example I have two notebooks (each with only one code cell):

notebook-1.ipynb

a = 13

notebook-2.ipynb

print(a)

I would like to have one kernel that first will execute notebook-1.ipynb and then will execute notebook-2.ipynb.

What I'm currently trying:

import asyncio
from nbformat import read as nbread
from jupyter_client import AsyncKernelManager
from nbclient import execute

async def start():
    nb = nbread("notebook-1.ipynb", as_version=4)
    nb2 = nbread("notebook-2.ipynb", as_version=4)


    kernel_name = "python3"
    km = AsyncKernelManager(kernel_name=kernel_name)
    await km.start_kernel()

    # execute the notebook-1
    execute(nb, km=km)
    print(nb.cells)

    # execute the notebook-2
    execute(nb2, km=km)
    print(nb2.cells)


if __name__ == "__main__":
    asyncio.run(start())

I got an error:

raceback (most recent call last):
  File "kernel2.py", line 25, in <module>
    asyncio.run(start())
  File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
    return future.result()
  File "kernel2.py", line 16, in start
    execute(nb, km=km)
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nbclient/client.py", line 1204, in execute
    return NotebookClient(nb=nb, resources=resources, km=km, **kwargs).execute()
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nbclient/util.py", line 84, in wrapped
    return just_run(coro(*args, **kwargs))
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nbclient/util.py", line 62, in just_run
    return loop.run_until_complete(coro)
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nest_asyncio.py", line 89, in run_until_complete
    return f.result()
  File "/usr/lib/python3.8/asyncio/futures.py", line 175, in result
    raise self._exception
  File "/usr/lib/python3.8/asyncio/tasks.py", line 280, in __step
    result = coro.send(None)
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nbclient/client.py", line 648, in async_execute
    assert self.kc is not None
AssertionError

How to reuse existing kernel with nbclient?

@davidbrochart
Copy link
Member

Thanks for reporting @pplonski.
I opened #239, it would be great if you could review it.

@pplonski
Copy link
Author

@davidbrochart thanks for looking into this!

I've installed nbclient from your repo:

pip install -U git+https://github.com/davidbrochart/nbclient.git@main

and run the script and got the same error:

Traceback (most recent call last):
  File "kernel2.py", line 25, in <module>
    asyncio.run(start())
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "kernel2.py", line 16, in start
    execute(nb, km=km)
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nbclient/client.py", line 1204, in execute
    return NotebookClient(nb=nb, resources=resources, km=km, **kwargs).execute()
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nbclient/util.py", line 84, in wrapped
    return just_run(coro(*args, **kwargs))
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nbclient/util.py", line 62, in just_run
    return loop.run_until_complete(coro)
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nest_asyncio.py", line 89, in run_until_complete
    return f.result()
  File "/usr/lib/python3.8/asyncio/futures.py", line 178, in result
    raise self._exception
  File "/usr/lib/python3.8/asyncio/tasks.py", line 280, in __step
    result = coro.send(None)
  File "/home/piotr/sandbox/python-dashboard/pdenv/lib/python3.8/site-packages/nbclient/client.py", line 648, in async_execute
    assert self.kc is not None
AssertionError

is the additional check in the code in the correct place?

@davidbrochart
Copy link
Member

You should install the fix_client branch:

pip install -U git+https://github.com/davidbrochart/nbclient.git@fix_client

@pplonski
Copy link
Author

@davidbrochart thank you!

It is working! Thank you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants