-
-
Notifications
You must be signed in to change notification settings - Fork 373
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
ipykernel disallows jupyter user to create/attach to event loop #548
Comments
@ddelange Can you explain why you don't just call await asyncio.sleep(0)? Your statement is correct, but also note that "await asyncio.sleep(0)" does not work with latest default IPython. This isn't really a bug, but rather a design choice. So, you could file a bug over there. In reality, I think the complaint here is more with the asyncio folks who have created a functional dichotomy in that sync functions can not call async functions except via a top level asyncio.run() |
The main reason why I think a cell should allow sync functions to attach/create a loop under the hood is for async libraries that provide sync wrappers somehow using In order for those sync wrappers to work in a notebook, the developers of those libraries would have to jump through hoops to work around this issue. For instance, But mostly developers will expect their code (working in pythyon/IPython) to run in notebooks as well. As a result, there are now numerous projects that require
As this issue does not occur in Python/IPython REPL but only in notebooks (as mentioned in various linked issues), I think there should be a solution where ipykernel does not reserve the sole right to create an event loop on the main thread, like the suggestions from @minrk. Using
|
I think this statement conflicts with the design goals of asyncio: " forcing every library that wraps async code to refactor purely for notebook compatibility is not a feasible solution in my opinion." Forcing the refactoring of code for compatibility is actually the intention of the asyncio designers. They discuss this in numerous different places. They believe that all async code need's to have the ability to execute in the context of async. They have specifically said that there should only be one global event loop per thread. Breaking this contract seems like a great way to generate a lot of confusion in the ecosystem and is no better than monkey patching. Try this:
|
Note that the asyncio docs specifically discourages it's users from calling get_event_loop. "Because this function has rather complex behavior (especially when custom event loop policies are in use), using the get_running_loop() function is preferred to get_event_loop() in coroutines and callbacks." https://docs.python.org/3/library/asyncio-eventloop.html Making an API public and then discouraging it's use is rather unnerving, but that is what we have. My general thought is a lot of grief is occurring and folks are laying concerns at the feet of application owners rather than the language designers themselves. Trying to implement around their contracts is just going to cause bugs and incompatibility and lack of backwards compatiblity. |
The language designers rejected addressing this here - https://bugs.python.org/issue33523 We can point fingers at individual libraries, but unless there is way of consistent finger pointing (ie, a contract) this is going to lead to a global mess. |
The current proposed solutions are changing asyncio libraries to work with ipykernel or modifying python. Is there a way to change ipykernel instead? |
Hi all, I'm following up on this. |
Hello,
as follow-up on jupyter/notebook#3397 (comment), here a minimal example that I expect to finish successfully when sent to the kernel by jupyter:
It runs fine in latest IPython which doesn't use tornado under the hood.
However,
notebook>=5.7.9
requirestornado>=5
and so when ipykernel executesloop = tornado.ioloop.IOLoop.current()
, an asyncio event loop will be created andrun_forever
by ipykernel.This consequently leaves the user without options (left aside
nest-asyncio
which works for most cases), as there is a running asyncio loop on startup.If IPython can run this simple snippet, shouldn't jupyter be able to do the same? Currently, any package that does a plain loop setup and teardown will crash in jupyter envs that have
tornado>=5
.The text was updated successfully, but these errors were encountered: