jayqi/reprexlite

Run async code directly

Opened this issue · 2 comments

jayqi commented

There are other tools that let users run async code directly. See this comment for details.

Right now, if you try to execute a code example with async code, you get something like

SyntaxError: 'async with' outside async function

This is probably because exec and eval are not run as async.

jayqi commented

False alarm. This seems to be because I tried running an httpx example that is not reproducible! It doesn't work in the Python REPL either.

>>> import httpx
>>> async with httpx.AsyncClient() as client:
...     r = await client.get('https://www.example.org/')
... 
  File "<stdin>", line 1
SyntaxError: 'async with' outside async function

The hello world example from the asyncio docs has no problem:

import asyncio

async def main():
    print('hello')
    await asyncio.sleep(1)
    print('world')

asyncio.run(main())
#> hello
#> world
jayqi commented

It looks like there's a real feature here, which is to natively run async code without needing asyncio.run.

Reference implementation for python -m asyncio: python/cpython@16cefb0

Support for this will likely involve some research. It may be the case that implementation is specific to asyncio, and if we wanted to support trio or curio it would need to work differently.

It would be cool if this worked automatically like IPython, but maybe will need to have an --asyncio flag that toggles the mode.