Reputation: 1214
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
Upvotes: 12
Views: 13514
Reputation: 5853
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()
Upvotes: 3