使用蓝图在 Quart 中导入 Flask 扩展

Importing a Flask extension in Quart using Blueprints

我目前正在构建一个基本应用程序,我在其中使用连接到主机 Raspberry Pi 的传感器进行测量。为此,我决定使用 Quart,因为它允许我在后台 运行 它们。我目前正在尝试使用缓存,以便 JS 前端可以使用数据(例如,用于下载、实时绘图)。 websockets 和其他请求。我想用 Flask-Caching 扩展来做到这一点,根据 pgjones,Quart 支持它。但是,一旦我尝试初始化扩展,我就会得到

Traceback (most recent call last):
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/bin/quart", line 8, in <module>
    sys.exit(main())
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/quart/cli.py", line 344, in main
    cli.main(args=args, prog_name=name)
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/quart/cli.py", line 235, in main
    return super().main(*args, **kwargs)
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/click/core.py", line 1653, in invoke
    cmd_name, cmd, args = self.resolve_command(ctx, args)
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/click/core.py", line 1700, in resolve_command
    cmd = self.get_command(ctx, cmd_name)
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/quart/cli.py", line 220, in get_command
    command = info.load_app().cli.get_command(ctx, name)  # type: ignore
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/quart/cli.py", line 72, in load_app
    module = import_module(import_name)
  File "/home/andromedan/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/run.py", line 7, in <module>
    app = create_app()
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/app/__init__.py", line 15, in create_app
    cache.init_app(app, config=config.CACHE)
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/flask_caching/__init__.py", line 220, in init_app
    self._set_cache(app, config)
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/flask_caching/__init__.py", line 251, in _set_cache
    app.extensions["cache"][self] = cache_factory(
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/flask_caching/backends/filesystemcache.py", line 95, in factory
    return cls(*args, **kwargs)
  File "/home/andromedan/kubo_sound/webapp/kubo_masking/venv/lib/python3.8/site-packages/flask_caching/backends/filesystemcache.py", line 76, in __init__
    os.makedirs(self._path)
  File "/home/andromedan/anaconda3/lib/python3.8/os.py", line 208, in makedirs
    head, tail = path.split(name)
  File "/home/andromedan/anaconda3/lib/python3.8/posixpath.py", line 103, in split
    p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not NoneType

追根溯源

cache.init_app(app, config=config.CACHE)

在我的应用工厂中。这是应用工厂

import quart.flask_patch

from quart import Quart
from config import Config

# import extensions instance
from flask_caching import Cache

cache = Cache()

def create_app(config=Config):
    app = Quart(__name__)
    app.config.from_object(config)

    # initialize extension instances
    cache.init_app(app, config=config.CACHE)
    
    # register blueprints of applications
    from app.main import main as main_bp
    app.register_blueprint(main_bp)

    from app.measurement import measurement as measurement_bp
    app.register_blueprint(measurement_bp)

    return app

和运行文件代码

# import the create app application factory
from app import create_app

# import the application config classes
from config import Config

app = create_app()

if __name__ == '__main__':
app.run()

CACHE 属性 Config 由字典 {'CACHE_TYPE': 'FileSystemCache'} 给出。

我尝试了其他选项,例如 cache = Cache(app, config=config.CACHE) 来初始化扩展,none 有效...

奖金问题:flask docs中提到的缓存类型有什么区别?

谢谢!

Config 中的 CACHE 属性缺失 CACHE_DIR,它将定义 FileSystemCache 存储缓存数据的位置。例如,如果 CACHE 属性 Config 是字典 {'CACHE_TYPE': 'FileSystemCache', 'CACHE_DIR': '/tmp'}