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

Async execute cells #1406

Closed
wants to merge 2 commits into from
Closed

Conversation

davidbrochart
Copy link
Member

# Apply rules from nbclient for where to apply execution counts
if execution_count and cell.cell_type == 'code' and cell.source.strip():
cell['execution_count'] = execution_count
return cell, resources

def preprocess_cell(self, cell, resources, index, **kwargs):
async def preprocess_cell(self, cell, resources, index, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other preprocessors' preprocess_cell are not async. Should we make an async version of that method and call it from async_execute_cell?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think we just need to ensure it is async, I'm pushing another commit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will most likely have consequences, because anyone overriding preprocess_cell in a non-async way will have surprises.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, since we now call ensure_async(preprocess_cell()), it can be async or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as discussed in person, for people who override preprocess_cell and then call super.preprocess_cell in their implementation without await this should be an issue, but I need to check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is an issue and it will break downstream projects like nbdev if preprocess_cell is made async in nbconvert

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should try because I don't think it is a problem with the ensure_async.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it will be an issue to mix:

In [8]: class A:
   ...:     async def foo(self):
   ...:         print("bar")
   ...: 

In [9]: class B(A):
   ...:     def foo(self):
   ...:         print("foo")
   ...:         A.foo(self)
   ...:         print("!")
   ...: 

In [10]: B().foo()
foo
<ipython-input-9-33f3ae08d87f>:4: RuntimeWarning: coroutine 'A.foo' was never awaited
  A.foo(self)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
!

Making the subclass aware of the parent being async but not being async itself on the override does weird things too:

In [12]: class B(A):
    ...:     def foo(self):
    ...:         print("foo")
    ...:         await A.foo(self)
    ...:         print("!")
    ...: 

In [13]: B().foo()
Out[13]: <coroutine object B.foo at 0x7f1d87179c40>

except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.async_execute())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may be just moving the issue here. run_until_complete will fail if the loop is already running.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, if nbconvert is used in a notebook for instance. But not if it is used from the CLI.

@SylvainCorlay
Copy link
Member

Note: it seems that the nest-asyncio author just pushed a fix: erdewit/nest_asyncio#34

If a patch release is published, we should probably just use that and close this.

@SylvainCorlay
Copy link
Member

Closing for now, since this approach will probably not work.

However, I would like to make a pass on the interface between nbclient and the execute preprocessor.

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 this pull request may close these issues.

The use of nest_asyncio makes nbconvert --to webpdf hang when used with --execute
3 participants