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

Make nest_asyncio optional #37

Merged
merged 14 commits into from
Mar 25, 2020
Merged

Make nest_asyncio optional #37

merged 14 commits into from
Mar 25, 2020

Conversation

davidbrochart
Copy link
Member

In nbclient, we chose to implement blocking methods such as execute by running their async counterparts (e.g. async_execute) in an event loop (with asyncio.run_until_complete), to avoid code duplication. This can be an issue in environments where an event loop is already running, such as a Jupyter Notebook. nest_asyncio solves this issue, but it doesn't play well with tornado (see tornadoweb/tornado#2753). This will impact applications such as Voila (which will use the async methods of nbclient and so won't need to nest event loops). We should for now make the use of nest_asyncio an opt-in.

@choldgraf
Copy link
Contributor

Correct me if I'm wrong, but this is also removing one of the more-common user-facing methods execute, no?

@davidbrochart
Copy link
Member Author

No, but I agree it looks like it. It's actually monkey-patched with the make_blocking function. Not sure this is acceptable though.

@davidbrochart
Copy link
Member Author

I removed the monkey-patching of NotebookClient. Now execute and execute_cell are explicitly declared and call run_blocking on their async counterparts.

@davidbrochart
Copy link
Member Author

Trigger CI.

@davidbrochart
Copy link
Member Author

Trigger CI.

@davidbrochart davidbrochart reopened this Mar 12, 2020
@choldgraf
Copy link
Contributor

Another user-facing question from me - does it mean that running this from within Jupyter Notebooks will require using the next_asyncio flag? I feel like this will be not be obvious to people. Is there any way to check whether we need to nest based on environment variables etc? Or a way to catch the error message and display something like "try running with next_asyncio = True?

@davidbrochart
Copy link
Member Author

davidbrochart commented Mar 12, 2020

Thanks for you comment @choldgraf, this last commit shows a meaningful message when nesting event loops seems to be needed.

@choldgraf
Copy link
Contributor

Nice - I like that, I think it'll be helpful for people that aren't familiar with some of these issues (which unfortunately is probably exactly the kind of person that would be calling execute from within a notebook environment)

@davidbrochart
Copy link
Member Author

I'm going to merge this PR soon, if nobody has objections.

Copy link
Contributor

@MSeal MSeal left a comment

Choose a reason for hiding this comment

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

Sorry, was on vacation and dealing with COVID lockdown so I'm only now catching up on threads. Solution seems fine, just want to preserve comments :)

nbclient/client.py Show resolved Hide resolved
nbclient/client.py Show resolved Hide resolved
@davidbrochart
Copy link
Member Author

Welcome back Matthew, I hope you are fine.
The last commit refactors the previous run_blocking into run_sync, which automatically inherits the docstring of the async method. Also, is_alive is now async in jupyter_client.

@davidbrochart
Copy link
Member Author

The failing tests are related to #38 (comment), which will be solved when we have a jupyter_client release.
Also test_many_parallel_notebooks randomly fails, we need to investigate that.

Copy link
Contributor

@MSeal MSeal left a comment

Choose a reason for hiding this comment

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

There was a minor conflict with your other PR I just merged, +1 to merge after the conflict is fixed. I'll see what's left after to keep from #34 and update that PR. Once this and 34 merges I think we can make a release.

test_parallel does fail randomly still ... as I currently understand it there's still a kernel socket race condition (I've fixed a number of them now) in how ports are registered. This might be a jupyter_client issue to track down tackle again.

@MSeal
Copy link
Contributor

MSeal commented Mar 20, 2020

Welcome back Matthew, I hope you are fine.

Yep all good so far, went through some exposed places getting home but no symptoms as of 3 days in. Disconnected from my inbox for a bit was all.

@davidbrochart
Copy link
Member Author

I will merge this PR soon, if no objection.

@MSeal MSeal merged commit 9c941f9 into jupyter:master Mar 25, 2020
@MSeal
Copy link
Contributor

MSeal commented Mar 25, 2020

merged

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.

None yet

3 participants