Skip to content

Add an option to run an asyncio event loop automatically #9166

Closed

Description

@sametmax

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)

Activity

  1. takluyver commented on Jan 25, 2016

    @takluyver
    Member

    I've whipped together a quick extension that adds an %%asyncio cell magic. You can use it like this:

    In [1]: %load_ext asyncio_magic
    
    In [2]: import aiohttp
    
    In [3]: %%asyncio
       ...: r = await aiohttp.get('http://example.com')
       ...: print(await r.text())
       ...: 
    <!doctype html>
    ...
    

    It's just turning any await x into asyncio.get_event_loop().run_until_complete(x).

    You can get the extension here: https://gist.github.com/takluyver/b9663b08ac9a4472afa6

  2. modified the milestones: 4.1, wishlist on Jan 25, 2016
  3. sametmax commented on Jan 26, 2016

    @sametmax
    Author

    Cool ! Works well for my current use case.

  4. modified the milestones: 5.0, wishlist on Feb 3, 2016
  5. takluyver commented on Feb 3, 2016

    @takluyver
    Member

    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 await in IPython cells, because await is only valid inside an async def function.

    I think this should be relatively straightforward once we switch to prompt_toolkit, so milestoned for 5.0.

  6. sametmax commented on Mar 7, 2016

    @sametmax
    Author

    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.

  7. takluyver commented on Mar 7, 2016

    @takluyver
    Member

    Hmm, I'm surprised at that. It should work. I'll have a play with it in a bit.

  8. takluyver commented on Mar 7, 2016

    @takluyver
    Member

    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:

    In [6]: %%asyncio
       ...: r = await aiohttp.get('http://example.com')
       ...: 
    
    In [7]: r
    Out[7]: 
    <ClientResponse(http://example.com/) [200 OK]>
    ...
    

    Can you give an example of what doesn't work that should do?

  9. sametmax commented on Mar 7, 2016

    @sametmax
    Author

    I can't reproduce the issue, sorry for wasting your time.

  10. Carreau commented on May 19, 2016

    @Carreau
    Member

    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.

  11. 4 remaining items

  12. Gr1N commented on Nov 12, 2016

    @Gr1N

    @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

  13. takluyver commented on Nov 13, 2016

    @takluyver
    Member

    👍 thanks @Gr1N !

  14. modified the milestones: 6.0, 7.0 on Feb 9, 2017
  15. graingert commented on Feb 14, 2017

    @graingert
    Contributor

    @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

  16. takluyver commented on Feb 15, 2017

    @takluyver
    Member

    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.

  17. jansegre commented on Jun 21, 2017

    @jansegre

    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 type await 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.

  18. graingert commented on Jun 21, 2017

    @graingert
    Contributor

    @jansegre I agree

  19. Carreau commented on Jun 22, 2017

    @Carreau
    Member

    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)

  20. Carreau commented on Aug 28, 2018

    @Carreau
    Member

    I believe #11265 is almost complete and would fix this issue almost completely (except background tasks). Testers welcome.

  21. Carreau commented on Sep 7, 2018

    @Carreau
    Member

    That should now be fixed by #11265; it will need further patches in IPykernel to work with the notebook, but it is in progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions