Closed
Labels
Milestone
Description
Experimenting with asyncio righ now is a pain. If I want just to use aiohttp to make a get request, I need to do:
import asyncio
import aiohttp
async def main():
content = await asyncio.get('http://foo.com').content
print(content)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Compare to doing the same with requests:
import requets
print(requests.get('http://foo.com').content)
I wish ipython would have an --asyncio option which starts an event loop with the shell and run any command in it.
So my example with aiohttp would become:
$ ipython --asyncio
Then:
import aiohttp
print(await asyncio.get('http://foo.com').content)
Reacted by Roey Darwish Dror, Sebastian Hanula, javier, Colin Nichols, chrysn, Fabio Cerqueira, Vinicius Fortuna, Razzi Abuissa, Lev Maximov, Vladislav and 4 more
Reacted by Igor
Activity
takluyver commentedon Jan 25, 2016
I've whipped together a quick extension that adds an
%%asynciocell magic. You can use it like this:It's just turning any
await xintoasyncio.get_event_loop().run_until_complete(x).You can get the extension here: https://gist.github.com/takluyver/b9663b08ac9a4472afa6
sametmax commentedon Jan 26, 2016
Cool ! Works well for my current use case.
takluyver commentedon Feb 3, 2016
I'm leaving this open because it does make sense to integrate with the asyncio event loop - although doing so won't automatically let you use
awaitin IPython cells, becauseawaitis only valid inside anasync deffunction.I think this should be relatively straightforward once we switch to prompt_toolkit, so milestoned for 5.0.
sametmax commentedon Mar 7, 2016
Indeed. One thing I noticed with the current solution is that the local scope is lost. I thing it needs some adjustements to get the local variables to be available such as saving locals() and merging it back when we execute the await statement.
takluyver commentedon Mar 7, 2016
Hmm, I'm surprised at that. It should work. I'll have a play with it in a bit.
takluyver commentedon Mar 7, 2016
I'm not sure what you mean about losing the local scope. I tried what I thought you meant wasn't working, and it did work:
Can you give an example of what doesn't work that should do?
sametmax commentedon Mar 7, 2016
I can't reproduce the issue, sorry for wasting your time.
Carreau commentedon May 19, 2016
I'm bumping that to 6.0 as it is likely a new feature that will be easier to implement once we can make more assumption on wether or not we are on Python 3.
4 remaining items
Gr1N commentedon Nov 12, 2016
@takluyver thanks for the gist, I created and uploaded to PyPI a package based on your gist, in case, if anyone wants to use it: https://github.com/Gr1N/asyncio-ipython-magic
takluyver commentedon Nov 13, 2016
👍 thanks @Gr1N !
graingert commentedon Feb 14, 2017
@takluyver can you re-look at this, it would be really handy. Especially as CPython don't plan to add this feature: https://bugs.python.org/issue22412
takluyver commentedon Feb 15, 2017
I think the arguments why Python doesn't want to do it broadly go for IPython as well. I wrote the initial version of the magic mostly because it was an interesting challenge, and I think it makes a fairly neat extension, but I don't see us enabling it by default in IPython.
jansegre commentedon Jun 21, 2017
I think it makes somewhat more sense to have this in IPython (rather than on CPython) mostly because there is so much more IPython provides (like
_*variables). But I guess it'd still be somewhat surprising for newcomers to typeawait foo()work in IPython but not when writing a script. Still, being able to try out what I would type inside an async function would make prototyping async code so much better.graingert commentedon Jun 21, 2017
@jansegre I agree
Carreau commentedon Jun 22, 2017
If we do that it will probably be opt-in, see #10390 as an experiement. One of the reason we won't star an asyncio loop automatically is that asycio is not the only coroutine runner (I'm not using the term Loop on purpose), I would highly recommend to look at curio and trio, but tornado is also a possibility. So users would likely need to say explicitely which runner thy wish, as you need a runner compatible with your library. So far #10390 has not moved fwd because it needs testing and fixes for running with IPykernel (which uses tornado eventloop)
Carreau commentedon Aug 28, 2018
I believe #11265 is almost complete and would fix this issue almost completely (except background tasks). Testers welcome.
Carreau commentedon Sep 7, 2018
That should now be fixed by #11265; it will need further patches in IPykernel to work with the notebook, but it is in progress.