可能会在烧瓶中循环导入?

时间:2019-07-31 15:24:48

标签: python flask

我有这样的结构:

├── app
│   ├── __init__.py
│   └── views.py
├── requirements.txt
├── sources
│   └── passport
│       ├── field_mapping.
│       ├── listener.py
│       ├── main.py

这是我的初始化文件:

from flask import Flask

app = Flask(__name__)

from app import views

我的观点文件。这是发送纯文本的最佳方法吗?

from app import app
from flask import Response
from sources.app_metrics import meters

# from sources.passport.main import subscription_types

@app.route('/metrics')
def metrics():
    def generateMetrics():
        metrics = ""
        for subscription in ["something", "some other thing"]:
            metrics += "thing_{}_count {}\n".format(subscription, meters[subscription].get()['count'])
        return metrics
    print(generateMetrics())
    return Response(generateMetrics(), mimetype='text/plain')

我的sources/passport/main文件如下:

subscription_types = ["opportunity", "account", "lead"]

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    ...

    for subscription in subscription_types():

在运行export FLASK_ENV=app/__init__.py之前,我还运行过flask app

当我访问/metrics时,出现类似循环依赖错误的错误。

当我取消注释视图,文件中的导入注释时,发生错误。 将subscription_types提取到变量中,然后将其导入似乎是造成此问题的原因。

我的堆栈跟踪:

 File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "/Users/jwan/extract/app/__init__.py", line 5, in <module>
    from app import views
  File "/Users/jwan//extract/app/views.py", line 5, in <module>
    from sources.passport.main import subscription_types
  File "/Users/jwan/extract/sources/passport/main.py", line 3, in <module>
    from sources.passport.listener import subscribe, close_subscriptions
  File "/Users/jwan/extract/sources/passport/listener.py", line 18, in <module>
    QUEUE = boto3.resource("sqs").get_queue_by_name(QueueName=CONFIG["assertions_queue"][ENV])

botocore.errorfactory.QueueDoesNotExist: An error occurred (AWS.SimpleQueueService.NonExistentQueue) when calling the GetQueueUrl operation: The specified queue does not exist for this wsdl versio

我的sources/passport/listener文件在第18行有此文件:

import gzip
import log
from os import getenv
from sources.passport.normalizer import normalize_message
from sources.app_metrics import meters

QUEUE = boto3.resource("sqs").get_queue_by_name(QueueName=CONFIG["assertions_queue"][ENV])

0 个答案:

没有答案