You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code调试Python无法进入类方法断点的问题求助

问题描述

我基于AWS文档编写了Python代码用于消费Kinesis流,但仅在VS Code调试环节出现问题。代码结构为:导入依赖库、定义带__init__和consuming方法的Consume类、创建类实例、调用实例的consuming方法。我在最后一行consumer.consuming(5)设置断点,调试触发后点击Step In,程序直接终止,无法进入类方法内部。

本地环境:Windows 11,无容器
launch.json配置如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

代码如下:

import boto3
import json
import time
import datetime
from botocore.exceptions import ClientError

class Consume():
    def __init__(self,kinesis_client,stream_name):
          self.kinesis_client=kinesis_client
          self.stream_name=stream_name
          self.details=kinesis_client.describe_stream(StreamName='input_stream')

    def consuming(self,max_records):
        try:
            response = self.kinesis_client.get_shard_iterator(
                StreamName=self.stream_name, ShardId=self.details['Shards'][0]['ShardId'],
                ShardIteratorType='LATEST')
            shard_iter = response['ShardIterator']
            record_count = 0
            while record_count < max_records:
                response = self.kinesis_client.get_records(
                    ShardIterator=shard_iter, Limit=10)
                shard_iter = response['NextShardIterator']
                records = response['Records']
                print("Got {} records.".format(len(records)))
                record_count += len(records)
                yield records
        except ClientError:
            print("Couldn't get records from stream {}.".format(self.stream_name))
            raise

kinesis_client=boto3.client('kinesis',
                     aws_access_key_id=<some_hidden_string>,
                     aws_secret_access_key=<some_hidden_string>)

consumer=Consume(kinesis_client,stream_name='input_stream')
consumer.consuming(5)
问题原因与解决方法
  • 核心原因:consuming是带yield关键字的生成器函数,调用consumer.consuming(5)只会创建生成器对象,不会立即执行函数内部逻辑,程序执行到这一行后无后续操作,直接终止,导致调试无法进入方法。

  • 解决办法:

    1. 触发生成器执行:必须通过遍历或调用next()来启动函数执行,修改最后一行代码:
      # 方式1:遍历生成器,完整执行逻辑
      for records in consumer.consuming(5):
          # 可添加处理records的逻辑,或仅保留pass触发执行
          pass
      # 方式2:调用next()触发单次迭代,若要完整执行需多次调用
      gen = consumer.consuming(5)
      next(gen)
      
    2. 调试配置优化:当前launch.json中justMyCode设为true,可能导致VS Code将部分自定义代码判定为非用户代码,影响调试。可改为false确保能进入类方法:
      "justMyCode": false
      

内容的提问来源于stack exchange,提问作者Eugenio.Gastelum96

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 21:55:01