question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

asyncio will be running by default with tornado 5

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

6reactions
fortunacommented, Mar 31, 2018

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())
4reactions
Carreaucommented, Nov 1, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bridge between asyncio and Tornado
Tornado is designed to use a selector-based event loop. On Windows, where a proactor-based event loop has been the default since Python 3.8,...
Read more >
Run tornado.testing.AsyncTestCase using asyncio event loop
I have an asyncio based class which I want to unit test. Using tornado.testing.AsyncTestCase this works quite well and easily. However, one ...
Read more >
Developing with asyncio — Python 3.11.1 documentation
By default asyncio runs in production mode. In order to ease the development asyncio has a debug mode. There are several ways to...
Read more >
Tornado - freshcode.club
Pattern is to start the event loop with `asyncio.run`. More detailed migration. ... A default `User-Agent` of `Tornado/ VERSION` is now used if...
Read more >
IPython 7.0, Async REPL - Jupyter Blog
The default code will run in the existing asyncio/tornado loop that runs the kernel. Integration with Trio and Curio is still available, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found