Issue
I am wondering if there is a way to call async
functions with await
directly from python REPL ?
So having following code as example:
import aiohttp
import asyncio
async def somefunc():
async with aiohttp.ClientSession() as session:
host_url = 'https://somehost'
async with session.get(host_url) as resp:
response = await resp.json()
return response
What I am looking for is an easy way to debug similar functions, basically I'd like to
>>> result = await somefunc()
I am aware that this can be done with
import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(somefunc())
But using this approach I would need to either alter the body of somefunc to log the result of the response or use additional async function such as below to print the result
async def result_print():
result = await somefunc()
print(result)
loop = asyncio.get_event_loop()
loop.run_until_complete(result_print())
Summarising the things above, I am looking for a more convenient way to await async functions in the python REPL, maybe a custom REPL implementation that allows
>>> result = await somefunc()
Or possible there's a way to do that in python default repl that I am not aware of ?
Solution
The behavior you want is available if you launch the REPL using
python -m asyncio
Answered By - dirn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.