當前位置: 首頁>>代碼示例>>Python>>正文


Python g.start方法代碼示例

本文整理匯總了Python中flask.g.start方法的典型用法代碼示例。如果您正苦於以下問題:Python g.start方法的具體用法?Python g.start怎麽用?Python g.start使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在flask.g的用法示例。


在下文中一共展示了g.start方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: init_app

# 需要導入模塊: from flask import g [as 別名]
# 或者: from flask.g import start [as 別名]
def init_app(app):

    @app.before_request
    def record_request_details():
        CONCURRENT_REQUESTS.inc()

        g.start = monotonic()
        g.endpoint = request.endpoint

    @app.after_request
    def after_request(response):
        CONCURRENT_REQUESTS.dec()

        response.headers.add('Access-Control-Allow-Origin', '*')
        response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
        response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
        return response

    @app.errorhandler(Exception)
    def exception(error):
        app.logger.exception(error)
        # error.code is set for our exception types.
        msg = getattr(error, 'message', str(error))
        code = getattr(error, 'code', 500)
        return jsonify(result='error', message=msg), code

    @app.errorhandler(WerkzeugHTTPException)
    def werkzeug_exception(e):
        return make_response(
            jsonify(result='error', message=e.description),
            e.code,
            e.get_headers()
        )

    @app.errorhandler(404)
    def page_not_found(e):
        msg = e.description or "Not found"
        return jsonify(result='error', message=msg), 404 
開發者ID:alphagov,項目名稱:notifications-api,代碼行數:40,代碼來源:__init__.py

示例2: pre_request

# 需要導入模塊: from flask import g [as 別名]
# 或者: from flask.g import start [as 別名]
def pre_request():
    global database
    database.db.initializeSession()
    g.start = time.time() 
開發者ID:grin-pool,項目名稱:grin-pool,代碼行數:6,代碼來源:api.py

示例3: after_request

# 需要導入模塊: from flask import g [as 別名]
# 或者: from flask.g import start [as 別名]
def after_request(response):
    global LOGGER
    timediff = time.time() - g.start
    debug and LOGGER.warn("Exec time: {}".format(str(timediff)))
    return response 
開發者ID:grin-pool,項目名稱:grin-pool,代碼行數:7,代碼來源:api.py

示例4: init_app

# 需要導入模塊: from flask import g [as 別名]
# 或者: from flask.g import start [as 別名]
def init_app(app):
    def start_timer():
        request_id = request.headers.get("X-Request-ID", str(uuid4()))
        g.logger = current_app.logger.bind(request_id=request_id)
        g.request_id = request_id
        g.start = datetime.now()

    app.before_request(start_timer)

    def log_request(response):
        if request.path == "/favicon.ico":
            return response

        now = datetime.now()

        if hasattr(g, "start"):
            duration = int(round((now - g.start).microseconds / 1000, 2))
        else:
            duration = -1

        user_ip = request.headers.get("X-Forwarded-For", request.remote_addr)
        host = request.host.split(":", 1)[0]

        log_params = {
            "method": request.method,
            "path": request.path,
            "status": response.status_code,
            "duration": duration,
            "ip": user_ip,
            "host": host,
        }

        request_id = getattr(g, "request_id", None)

        if request_id:
            log_params["request_id"] = request_id

        if response.status_code < 400:
            current_app.logger.info("Request succeeded", **log_params)
        elif response.status_code < 500:
            current_app.logger.info("Bad Request", **log_params)
        else:
            current_app.logger.error("Internal Server Error", **log_params)

        return response

    app.after_request(log_request) 
開發者ID:fastlane-queue,項目名稱:fastlane,代碼行數:49,代碼來源:metrics.py


注:本文中的flask.g.start方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。