【问题标题】:Flask Max Recursion DepthFlask 最大递归深度
【发布时间】:2015-06-08 03:26:34
【问题描述】:

我有一个 max recursion depth 错误让我抓狂

在烧瓶__init__.py 文件中我有:

@app.route('/vpcs')
@app.route('/vpcs/<vpc>')
def getVPCs(vpc=False):
    """
    Get the vpcs.
    """
    results = getVPCs()
    return jsonify(**results)

它从以下位置调用函数:

def getVPCs():
   """
   This is the function that loops through the accounts and regions and does all the connections.
   """

使用帐户密钥循环访问配置文件的函数

   accounts = getAccountCreds()

   vpc_list = []
   try:

遍历每个区域 ... # 每个区域 区域中的 r:

遍历每个帐户

       # for each of the 8,yes 8, AWS accounts...
       for account,keys in accounts.iteritems():
         conn = VPCConnection(region=ec2.get_region(r),aws_access_key_id=keys[0],aws_secret_access_key=keys[1])

获取每个账户的所有 VPC ...

          vpcs = conn.get_all_vpcs()
          if formatVPC(account,vpcs[0]):
                vpc_list.append(formatVPC(account,vpcs[0]))
            return vpc_list
  except boto.exception.BotoServerError, e:
    print e

也叫:

def formatVPC(account,instance):
    """
    Function to format VPC data. 
    Keys we want: 
    - id
    - instance_tenancy
    - tags
    - region.name
    - region.connection
    - region.endpoint
    - state
    - cidr_block
    """
    result_dict = {}
    if instance:
        result_dict['account'] = account
        result_dict['id'] = instance.id
        result_dict['cidr_block'] = instance.cidr_block
        result_dict['instance_tenancy'] = instance.instance_tenancy
        result_dict['region'] = {'name':instance.region.name,'connection':   instance.region.connection,'endpoint':instance.region.endpoint}

        if result_dict:
            return result_dict

错误:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)

【问题讨论】:

  • getVPCs() 永远调用自己。

标签: python python-2.7 recursion flask depth


【解决方案1】:

它从以下位置调用函数:

不,来自您的 sn-p 的 getVPCs 只是自称:

@app.route('/vpcs')
@app.route('/vpcs/<vpc>')
def getVPCs(vpc=False):
    """
    Get the vpcs.
    """
    results = getVPCs()  # Here.
    return jsonify(**results)

并且递归地这样做,除了recursion limit之外没有任何“边界”,这导致RuntimeErrormaximum recursion depth exceededyouve提到。

更改其中一个函数的名称(当然,必要时调整调用)。

【讨论】:

    【解决方案2】:

    https://docs.python.org/2/library/sys.html

    sys.getrecursionlimit() 返回递归限制的当前值,Python 解释器堆栈的最大深度。此限制可防止无限递归导致 C 堆栈溢出和 Python 崩溃。可以通过setrecursionlimit()来设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      相关资源
      最近更新 更多