逾時後執行
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
這個範例會示範在 GCF 呼叫逾時後應 (但不會) 執行的程式碼。
程式碼範例
Java
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Node.js
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Python
如要驗證 Cloud Run 函式,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates code samples in Go, Java, Node.js, and Python that are designed to execute for two minutes, showcasing how Cloud Functions might time out before completion.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code examples highlight the potential for a function's execution to be interrupted by a timeout if the set duration is less than two minutes, as each sample includes a two-minute delay.\u003c/p\u003e\n"],["\u003cp\u003eThe samples use \u003ccode\u003etime.Sleep\u003c/code\u003e in Go, \u003ccode\u003eTimeUnit.MINUTES.sleep\u003c/code\u003e in Java, \u003ccode\u003esetTimeout\u003c/code\u003e in Node.js, and \u003ccode\u003etime.sleep\u003c/code\u003e in Python, to simulate a long-running process that could be cut short by a timeout.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication for Cloud Run functions is achieved through setting up Application Default Credentials, with a link provided for further instructions on local development environment authentication.\u003c/p\u003e\n"],["\u003cp\u003eThe content directs users to the Google Cloud sample browser for further exploration of code samples across various Google Cloud products.\u003c/p\u003e\n"]]],[],null,["# Execution after timeout\n\nSample that demonstrates code that should (but won't) execute after a GCF call times out.\n\nCode sample\n-----------\n\n### Go\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Package tips contains tips for writing Cloud Functions in Go.\n package tips\n\n import (\n \t\"fmt\"\n \t\"log\"\n \t\"net/http\"\n \t\"time\"\n\n \t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n )\n\n func init() {\n \tfunctions.HTTP(\"Timeout\", Timeout)\n }\n\n // Timeout sleeps for 2 minutes and may time out before finishing.\n func Timeout(w http.ResponseWriter, r *http.Request) {\n \tlog.Println(\"Function execution started...\")\n \ttime.Sleep(2 * time.Minute)\n \tlog.Println(\"Function completed!\")\n \tfmt.Fprintln(w, \"Function completed!\")\n }\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n package functions;\n\n import com.google.cloud.functions.HttpFunction;\n import com.google.cloud.functions.HttpRequest;\n import com.google.cloud.functions.HttpResponse;\n import java.io.BufferedWriter;\n import java.io.IOException;\n import java.util.concurrent.TimeUnit;\n import java.util.logging.Logger;\n\n public class AfterTimeout implements HttpFunction {\n private static final Logger logger = Logger.getLogger(AfterTimeout.class.getName());\n\n // Simple function to return \"Hello World\"\n @Override\n public void service(HttpRequest request, HttpResponse response)\n throws IOException, InterruptedException {\n logger.info(\"Function running...\");\n TimeUnit.MINUTES.sleep(2);\n\n // May not execute if function's timeout is \u003c2 minutes\n logger.info(\"Function completed!\");\n BufferedWriter writer = response.getWriter();\n writer.write(\"Function completed!\");\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const functions = require('@google-cloud/functions-framework');\n\n /**\n * HTTP Cloud Function that may not completely\n * execute due to function execution timeout\n *\n * @param {Object} req Cloud Function request context.\n * @param {Object} res Cloud Function response context.\n */\n functions.http('afterTimeout', (req, res) =\u003e {\n setTimeout(() =\u003e {\n // May not execute if function's timeout is \u003c2 minutes\n console.log('Function running...');\n res.end();\n }, 120000); // 2 minute delay\n });\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import time\n\n import functions_framework\n\n\n @functions_framework.http\n def timeout(request):\n print(\"Function running...\")\n time.sleep(120)\n\n # May not execute if function's timeout is \u003c2 minutes\n print(\"Function completed!\")\n return \"Function completed!\"\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]