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
Added asyncio support to cell magic #212
Conversation
To test the code, just add this to a notebook: # Cell 0
%load_ext pyinstrument
# Cell 1
%%pyinstrument
import time
import asyncio
time.sleep(0.1)
await asyncio.sleep(0.1) |
Thanks @wolph. This is cool. Do you think it would be possible to run this in the same thread, rather in a separate one? Perhaps by starting a asyncio run loop before the execution? I'm just wondering if things like threading.locals and contextvars could introduce bugs to users' programs. |
That's not easily possible due to the upstream bug unfortunately. IPython needs to run its own event event loop and by default One option would be to monkey-patch asyncio to allow for this, but I'm not sure if that's a better solution: https://github.com/erdewit/nest_asyncio |
Gotcha. Well, maybe we can make it a different magic, perhaps |
This hack is already conditional of having asyncio enabled so we could default to having asyncio disabled. I don't see a scenario where that would break existing behaviour for people, unless they already manually set the asyncio parameter |
@joerick I've disabled async mode by default now. To run the cell magic normally people can use:
For asyncio they can use:
|
Great! Yeah this'll work. Last thing - if users don't know about this and try to do an |
I tweaked the detection logic to make a bit stricter - hopefully this works. |
It seems to work great for me :) |
The current version of the pyinstrument magic does not support async code at all. It doesn't matter whether you have
async_mode
set toenabled
ordisabled
, it will break because it will try to run in a new event loop.This seems to be a known upstream issue but I don't think anyone is working on it: ipython/ipython#11314
This modification is certainly very hacky, but it's working quite well for me and I don't foresee any issues with it.
If it does cause issues, it doesn't do anything when using
--async_mode=disabled
:)