Skip to content

asyncio will be running by default with tornado 5 #11030

Closed
datarobot-community/af-component-agent
#257

Description

@minrk

When tornado 5 is released, which is scheduled to be this month, the tornado ioloop will run on asyncio by default. For the most part, this is nice because asyncio will always be running (#9166), but it's also tricky because simple cells like this that assume asyncio isn't running will stop working:

loop = asyncio.get_event_loop()
# will fail because loop is already running:
loop.run_until_complete(asyncio.sleep(1))

Without something like #10390, there won't be an easy way that I can see for users to run asyncio code in IPython, other than putting them in a background thread where they can run it in an eventloop:

aio_pool = ThreadPoolExecutor(1)

def init_loop():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    return loop

aio_loop = aio_pool.submit(init_loop).result()

async def mycoro():
    await asyncio.sleep(1)
    return 5

result = aio_pool.submit(lambda : aio_loop.run_until_complete(mycoro())).result()

and #10390 still needs to handle the fact that asyncio will be running, which I don't think it does right now.

Activity

  1. minrk commented on Mar 28, 2018

    @minrk
    MemberAuthor

    Here's a quick hack to provide an %%asyncio magic running async functions in the aio_thread:

    import asyncio
    
    from concurrent.futures import ThreadPoolExecutor
    import textwrap
    
    aio_pool = ThreadPoolExecutor(1)
    
    def init_loop():
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        return loop
    
    aio_loop = aio_pool.submit(init_loop).result()
    
    async def mycoro():
        await asyncio.sleep(1)
        return 5
    
    result = aio_pool.submit(lambda : aio_loop.run_until_complete(mycoro())).result()
    
    ip = get_ipython()
    def asyncio_magic(line, cell):
        async_cell = "async def __async_f():\n%s" % textwrap.indent(cell, '    ')
        exec(async_cell, ip.user_ns)
        f = ip.user_ns['__async_f']
        
        result = aio_pool.submit(lambda : aio_loop.run_until_complete(f())).result()
    
    ip.register_magic_function(asyncio_magic, magic_kind='cell', magic_name='asyncio')
  2. fortuna commented on Mar 31, 2018

    @fortuna

    It would be great if this was made easier.

    I was able to at least find a simpler workaround:

    async def do_stuff():
        await asyncio.sleep(1)
        print("Done")
    
    asyncio.run_coroutine_threadsafe(do_stuff(), asyncio.get_event_loop())
  3. fortuna commented on Mar 31, 2018

    @fortuna

    Here is an alternative to above.

    One time setup:

    from concurrent.futures import ThreadPoolExecutor
    import asyncio
    LOOP = asyncio.new_event_loop()
    ThreadPoolExecutor().submit(LOOP.run_forever)

    Whenever you need async:

    asyncio.run_coroutine_threadsafe(coro, LOOP).result()
  4. mgeier commented on Apr 6, 2018

    @mgeier
    Contributor

    The code suggested by @fortuna didn't work for me (I don't know why, it was a more complicated situation than in the examples shown here, probably I just made a mistake), but [UPDATE: I indeed made a mistake, the code provided by @fortuna works fine] I came up with a slightly more concise variant of @minrk's original suggestion:

    import asyncio
    from concurrent.futures import ThreadPoolExecutor
    
    aio_pool = ThreadPoolExecutor(1)
    aio_loop = asyncio.new_event_loop()
    aio_pool.submit(asyncio.set_event_loop, aio_loop).result()
    
    async def mycoro():
        await asyncio.sleep(1)
        return 5
    
    result = aio_pool.submit(aio_loop.run_until_complete, mycoro()).result()

    I think this should be equivalent to the code at the top of this page, but I'm an asyncio beginner, so I might be completely wrong.

    BTW, if somebody is using asyncio.create_subprocess_exec(), I had to add this:

    asyncio.get_child_watcher().attach_loop(aio_loop)

    Without it, I got:

    RuntimeError: Cannot add child handler, the child watcher does not have a loop attached
    
  5. Carreau commented on Nov 1, 2018

    @Carreau
    Member

    We now have asyncio repl closing.

  6. fm75 commented on Nov 1, 2018

    @fm75
    Contributor

    Excellent! Is this in a release?

  7. Carreau commented on Nov 1, 2018

    @Carreau
    Member

    Yes, see https://ipython.readthedocs.io/en/stable/whatsnew/version7.html#ipython-7-0-0

    You can just await:

    In [1]: import asyncio
    
    In [2]: await asyncio.sleep(1)
    
    In [3]:
    

    And it works.

  8. fm75 commented on Nov 1, 2018

    @fm75
    Contributor

    Indeed! Awesome. Just testing in the-littlest-jupyterhub that I set up this week.
    Thank you.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions