Issue
I was playing around async code lately, when I tried to debug the code in PyCharm I saw some really strange behaviour I think it's because of the underlying architectureimport asyncio. This is the code I'm talking about.
async def compute(x, y):
print("Compute %s + %s ..." % (x, y))
await asyncio.sleep(1.0)
return x + y
tasks = [compute(x,x) for x in range(10)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
It's strange because when I set breakpoint inside the coroutine execution is never breaked and whole code completes running easily besides this I don't get much details of event loop(except some mess in the stack).
So here are my questions
- Is there any standards or some good practice on debugging async code?
- How to peek into execution flow of event loop?
- Why isn't it breaking inside the async function?
Solution
Just to add here as sample here:
async def compute(x, y):
print("Compute %s + %s ..." % (x, y))
await asyncio.sleep(1.0) # add breakpoint here and then run it in debug mode
return x + y
tasks = [compute(x,x) for x in range(10)]
loop = asyncio.get_event_loop()
loop.set_debug(True)
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
Answered By - Snehal Parmar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.