简体   繁体   English

烧瓶最大递归深度

[英]Flask Max Recursion Depth

I have a max recursion depth error that is driving me nuts 我有一个max recursion depth错误,这使我发疯

In the flask __init__.py file I have: 在烧瓶__init__.py文件中,我有:

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

It calls the functions from: 它从以下位置调用函数:

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

A function that loops through the config file with the account keys 使用帐户密钥循环遍历配置文件的功能

   accounts = getAccountCreds()

   vpc_list = []
   try:

Loop through each region ... # for each region for r in REGIONS: 循环遍历每个区域...#REGIONS中r的每个区域:

Loop through each of the accounts 循环浏览每个帐户

       # 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])

Get all the VPCs for each account ... 获取每个帐户的所有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

Which also calls: 这也称为:

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

Error: 错误:

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)

It calls the functions from: 它从以下位置调用函数:

Nope, the getVPCs from your snippet just calls itself: 不,您的摘录中的getVPCs自称:

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

and does so recursively, without any "boundary" other than recursion limit , which results in RuntimeError : maximum recursion depth exceeded you've mentioned. 并且以递归方式执行此操作,除了递归limit之外,没有任何“边界”,这会导致RuntimeErrormaximum recursion depth exceeded您提到的maximum recursion depth exceeded

Change one of these functions' names (and, of course, adjust the call if necessary). 更改这些函数的名称之一(当然,如有必要,请调整调用)。

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

sys.getrecursionlimit() Return the current value of the recursion limit, the maximum depth of the Python interpreter stack. sys.getrecursionlimit()返回递归限制的当前值,即Python解释器堆栈的最大深度。 This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python. 此限制可防止无限递归导致C堆栈溢出和Python崩溃。 It can be set by setrecursionlimit(). 可以通过setrecursionlimit()进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM