[英]How to debug async python code?
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.我最近在玩异步代码,当我尝试在 PyCharm 中调试代码时,我看到了一些非常奇怪的行为,我认为这是因为底层架构导入 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所以这是我的问题
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.