Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Cloud Functions
  • Getting started
    • Overview
    • Creating and executing functions
    • Creating a timer
    • Creating a trigger for Yandex Message Queue
    • Creating a trigger for Object Storage
    • Creating a trigger for Container Registry
    • Creating a trigger for Cloud Logs
    • Creating a trigger for IoT Core
  • Step-by-step instructions
    • All instructions
    • Using functions to get an IAM token for a service account
    • Managing rights to access functions
    • Managing functions
      • Creating a function
      • Managing function versions
      • Working in the code editor
      • Invoking a function
      • Updating a function
      • Viewing monitoring charts
      • Viewing the execution log
      • Deleting a function
    • Managing triggers
      • Getting information about a trigger
      • Creating a timer
      • Creating a trigger for Message Queue
      • Creating a trigger for Object Storage
      • Creating a trigger for Container Registry
      • Creating a trigger for Cloud Logs
      • Creating a trigger for Yandex IoT Core
      • Updating a trigger
      • Deleting a trigger
  • Concepts
    • Overview
    • Functions
    • Invoking a function
    • Runtime environment
      • Overview
      • Environment
      • Execution context
    • Builder
    • Triggers
      • Overview
      • Timer
      • Trigger for Message Queue
      • Trigger for Object Storage
      • Trigger for Container Registry
      • Trigger for Cloud Logs
      • Trigger for Yandex IoT Core
    • Log groups
    • Monitoring
    • Backups
    • Quotas and limits
  • Developing in Node.js
    • Overview
    • Managing dependencies
    • Request handlers
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Python
    • Overview
    • Managing dependencies
    • Request handlers
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Go
    • Overview
    • Managing dependencies
    • Request handlers
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in PHP
    • Overview
    • Managing dependencies
    • Request handlers
    • Invocation context
    • Logging
    • Handling errors
  • Developing in Bash
    • Overview
    • Request handlers
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Java
    • Overview
    • Java programming model
      • Overview
      • Using the Function interface to set a handler function
      • Using the YcFunction interface to set a handler function
      • Using the HttpServlet class to set a handler
      • Using the SpringBootApplication annotation to set a handler
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in R
    • Overview
    • R programming model
    • Building and managing dependencies
    • Request handler
    • context.md
    • Logging
    • Handling errors
  • Developing in C#
    • Overview
    • C# programming model
      • Overview
      • Using the Function interface to set a handler function
      • Using the YcFunction interface to set a handler function
    • Building and managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Use cases
    • Creating skills for Alice
  • Pricing policy
  • Access management
  • API Functions reference
    • Authentication in the API
    • gRPC
      • Overview
      • FunctionService
      • OperationService
    • REST
      • Overview
      • Function
        • Overview
        • create
        • createVersion
        • delete
        • get
        • getVersion
        • getVersionByTag
        • list
        • listAccessBindings
        • listOperations
        • listRuntimes
        • listTagHistory
        • listVersions
        • removeTag
        • setAccessBindings
        • setTag
        • update
        • updateAccessBindings
  • API Triggers reference
    • Authentication in the API
    • gRPC
      • Overview
      • TriggerService
      • OperationService
    • REST
      • Overview
      • Trigger
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • pause
        • resume
        • update
  • Questions and answers
  1. Developing in Python
  2. Request handlers

Request handler

  • Handler types
  • Synchronous handler
  • Asynchronous handler
  • Examples
    • HTTP request structure output

A request handler is a method used to handle each function invocation. When creating a function version, you should specify the entry point that consists of the file name and request handler name (for example, main.handler).

Note

At any given time, a single function instance processes only one request. This lets you use global variables without having to provide data integrity control.

When invoking the handler, the runtime passes the following arguments:

  1. The request body (the event parameter):

    • If the request body is a JSON document, it's converted to a dict using the json.loads method.
    • If the function was called with the integration=raw query string parameter, the HTTP request body is passed to the function as is, unhandled.
  2. The invocation context (the context parameter).

    The context contains the necessary information about the function version. The structure of this object is described in Invocation context.

Handler types

A function can use both synchronous and asynchronous request handlers.

Synchronous handler

To have the execution result returned, use the return statement or raise an exception using the raise statement. A synchronous function must return a result or throw an exception.

Asynchronous handler

A handler can be an async def asynchronous function. In this case you can use the following statements:

  • return: Returns the function response.
  • raise: Reports an error to the runtime environment.
  • await: Tracks the execution of asynchronous function invocations.

Note

The service only supports the asyncio library as a runtime environment for asynchronous functions.

To learn more about programming using the async/await syntax, see the relevant section of the documentation.

Examples

HTTP request structure output

The following function outputs the request structure and invocation context to both the execution log and function response:

import json


def handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps({
            'event': event,
        }),
    }
In this article:
  • Handler types
  • Synchronous handler
  • Asynchronous handler
  • Examples
  • HTTP request structure output
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC