erdewit / nest_asyncio

Patch asyncio to allow nested event loops
BSD 2-Clause "Simplified" License
693 stars 79 forks source link

Error when running multiple Jupyter cells: ERROR! Session/line number was not unique in database #9

Closed rodmendezp closed 5 years ago

rodmendezp commented 5 years ago

I am running multiples cells, where each of them run an async functions, and I want the cells to run consecutively, so I am using the function loop.run_until_complete to make sure each cell completes before running the next. Here is an example

import asyncio
import nest_asyncio
from timeit import default_timer

nest_asyncio.apply()

async def print_loop():
  print('{0:<30}{1:>20}'.format('Function', 'Completed at'))
  for i in range(3):
    elapsed = default_timer() - START_TIME
    completed_at = '{:5.2f}s'.format(elapsed)
    print('{0:<30}{1:>20}'.format('[LOOP] %d' % i, completed_at))
    time.sleep(0.5)

START_TIME = defualt_timer()

# This code will go on each cell
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(print_loop())
loop.run_until_complete(task)

The run_until_complete blocks the next cell, but when running 3 or more cells I get the error ERROR! Session/line number was not unique in database. History logging moved to new session And the output start appearing in differents cells

bugger_init And I get the following bugged_cells

erdewit commented 5 years ago

I remember this was a bug in the older Jupyter. It runs fine for me in Jupyter lab.

rodmendezp commented 5 years ago

I updated jupyter along with ipykernel and ipython and it works! Thanks, great package