Why are processes, threads, or coroutines not executed after being created in function code?

更新时间:
复制 MD 格式

Function Compute billing is based on requests. After a function sends a response, its instance is frozen. This prevents code in subthreads from executing. You can use one of the following three solutions to resolve this issue.

  • (Recommended) Separate the logic that requires asynchronous processing into a new function. In the original function, use the Function Compute software development kit (SDK) or an HTTP URL to asynchronously invoke the new function.

    For example, a synchronous request can complete a user registration, and an asynchronous process can then send a confirmation email. To implement this, move the email-sending logic to a new function. After the original function completes the user registration, use the Function Compute SDK in your code to asynchronously invoke the new email-sending function.

  • You can make the main thread wait for all subthreads to finish before it sends a response. This prevents the instance from being frozen before the subthread code can execute.

  • You can set the minimum number of instances for the function to 1 or more. In this case, you are billed at the rate for elastic instances in light hibernation (formerly idle) even when no requests are being processed. For more information, see Billing overview.