为什么 Python 用 vscode debug, step into 会跳转到意想之外的地方?

讨论 未结 9 47
ActualAvocado
ActualAvocado 会员 2022年4月17日 08:21 发表
<p>源码 <a href="https://docs.python.org/zh-cn/3/library/asyncio-future.html" rel="nofollow">python 协程 Futures 样例</a></p> <pre><code class="language-python">import asyncio async def set_after(fut, delay, value): # Sleep for *delay* seconds. await asyncio.sleep(delay) # Set *value* as a result of *fut* Future. print(fut._state) fut.set_result(value) print(fut._state) async def main(): # Get the current event loop. loop = asyncio.get_running_loop() # Create a new Future object. # 我在这里设置了断点 想看看 future 对象的状态什么时候 哪个代码把他置成 pending 了 fut = loop.create_future() print(fut._state) # Run "set_after()" coroutine in a parallel Task. # We are using the low-level "loop.create_task()" API here because # we already have a reference to the event loop at hand. # Otherwise we could have just used "asyncio.create_task()". loop.create_task( set_after(fut, 1, '... world')) print(fut._state) print('hello ...') # Wait until *fut* has a result (1 second) and print it. print(await fut) asyncio.run(main()) </code></pre> <p>断点如上面源码注释那样。 我已经实现把 vscode launch.json justMyCode 设置成 false 了。 即便如此,我在一步步 step into 的时候。</p> <pre><code class="language-python"># 从上面一步步 step into 下去遇到在 base_events.py 成员函数 # 这个成员函数没有用装饰器 def create_future(self): """Create a Future object attached to the loop.""" return futures.Future(loop=self) # 跳转到同一个文件下的 def get_debug(self): return self._debug # 再次 step into 跳转回来 def create_future(self): """Create a Future object attached to the loop.""" return futures.Future(loop=self) # 再次 step into 回到最开始的断点处 fut = loop.create_future() </code></pre> <p>我是在 windows ,下面用 vscode 调试的,python3.9</p> <p>之前也遇到类似的情况,step into 会跳转一个我完全不理解的地方,看不出来是谁调用的。。 请教一下 这个原因是什么呢?还有我怎么样才能看到真正的一步步执行的流程。(我现在还是不知道 future 什么时候被设置了 pending 状态)</p>
收藏(0)  分享
相关标签: 灌水交流
注意:本文归作者所有,未经作者允许,不得转载
9个回复
  • ActualAvocado
    2022年4月17日 08:21
    补充一下第一次跳到 getdebug 函数的时候的调用堆栈
    0 0
  • u823tg
    2022年4月17日 08:21
    这不是意想不到地方这是 asyncio 库里。 那几个调试按钮 or 快捷键,你 Google 下理解了就能看到一步步执行的流程
    0 0
  • ActualAvocado
    2022年4月17日 08:21
    #3 我知道是 asyncio 库,我目的是看里面是怎么把 future 对象设置成 pending 状态。 抱歉我这里没说清楚,我说的意想不到指的是我在 return futures.Future(loop=self) 这一步 step into 后,我预计会跳转 Future 对象的 init 方法过去,但是没有,跳转到了 get_debug 这个函数上了。后续也没有跳转到 Future 对象的 init 方法 而是直接返回到一开始设置断点的地方 fut = loop.create_future()
    0 0
  • u823tg
    2022年4月17日 09:07
    额,你看看下 future 类就知道了啊。 这个你还调试
    0 0
  • u823tg
    2022年4月17日 09:07
    再说真要调试也不是在这打断点。 你跟踪的应该是 决定 future 对象状态 的变量
    0 0