在课堂上使用烧瓶 [英] Using flask inside class

查看:82
本文介绍了在课堂上使用烧瓶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有很多线程.其中之一是烧瓶,用于实现(辅助)API.它使用时负载低,并且永远不会暴露在Internet上,因此内置flask Web服务器非常好.

我当前的代码如下:

class API:
    # ... all other stuff here, skipped
    def run():
        app = flask.Flask('API')

        @app.route('/cmd1')
        def cmd1():
            self.cmd1()

        @app.route('/cmd2')
        def cmd2()
            self.cmd2()

        app.run()

我觉得我做错了,因为所有文档都说在模块级别创建flask应用程序".但是我不想这样做-它与我的测试弄混了,并且API只是大型应用程序的一小部分,该应用程序具有自己的结构和协议(每个应用程序"是在一个或多个线程中运行的单独的类). /p>

如何在课堂上使用Flask?

解决方案

尽管此方法有效,但并不符合Flask样式指南.如果您需要在项目中包装Flask应用程序,请根据需要创建一个单独的类并添加应执行的功能

from flask import Flask, Response


class EndpointAction(object):

    def __init__(self, action):
        self.action = action
        self.response = Response(status=200, headers={})

    def __call__(self, *args):
        self.action()
        return self.response


class FlaskAppWrapper(object):
    app = None

    def __init__(self, name):
        self.app = Flask(name)

    def run(self):
        self.app.run()

    def add_endpoint(self, endpoint=None, endpoint_name=None, handler=None):
        self.app.add_url_rule(endpoint, endpoint_name, EndpointAction(handler))


def action():
    # Execute anything

a = FlaskAppWrapper('wrap')
a.add_endpoint(endpoint='/ad', endpoint_name='ad', handler=action)
a.run()

这里要注意一些事情:

  • EndpointAction应该是一个包装程序,它将执行您的功能并生成一个空的200响应.如果需要,您可以编辑功能
  • 端点处理程序可以是定义了__call__方法的任何内容
  • 端点名称应唯一,因为它代表视图名称
  • 无法在应用程序之后添加端点,因为一旦应用程序启动,线程将阻塞.您可以通过在单独的线程上运行应用程序来启用它,但是不建议动态更改URL映射,线程也不安全

I have application with many threads. One of them is flask, which is used to implement (axillary) API. It used with low load and never exposed to the Internet, so build-in flask web server is perfectly fine.

My current code looks like this:

class API:
    # ... all other stuff here, skipped
    def run():
        app = flask.Flask('API')

        @app.route('/cmd1')
        def cmd1():
            self.cmd1()

        @app.route('/cmd2')
        def cmd2()
            self.cmd2()

        app.run()

I feel I done it wrong, because all docs says 'create flask app at module level'. But I don't want to do this - it mess up with my tests, and API is small part of the larger application, which has own structure and agreements (each 'application' is separate class running in one or more threads).

How can I use Flask inside class?

解决方案

Although this works it doesn't feel compliant with the Flask style guide. If you need to wrap a Flask application inside your project, create a separate class to your needs and add functions that should be executed

from flask import Flask, Response


class EndpointAction(object):

    def __init__(self, action):
        self.action = action
        self.response = Response(status=200, headers={})

    def __call__(self, *args):
        self.action()
        return self.response


class FlaskAppWrapper(object):
    app = None

    def __init__(self, name):
        self.app = Flask(name)

    def run(self):
        self.app.run()

    def add_endpoint(self, endpoint=None, endpoint_name=None, handler=None):
        self.app.add_url_rule(endpoint, endpoint_name, EndpointAction(handler))


def action():
    # Execute anything

a = FlaskAppWrapper('wrap')
a.add_endpoint(endpoint='/ad', endpoint_name='ad', handler=action)
a.run()

Some things to note here:

  • EndpointAction is supposed to be a wrapper that will execute your function and generate an empty 200 response. If you want you can edit the functionality
  • The endpoint handler can be anything that has a __call__ method defined
  • The endpoint name should be unique as it represents a view name
  • Adding endpoints after the application is not possible as the thread will block once the application starts. You can enable it by running the application on a separate thread but changing the URL map on the fly is not advised, neither thread safe

这篇关于在课堂上使用烧瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆