forked from GoogleCloudPlatform/functions-framework-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_main.py
More file actions
62 lines (42 loc) · 1.52 KB
/
async_main.py
File metadata and controls
62 lines (42 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import asyncio
import logging
from functions_framework import execution_id
logger = logging.getLogger(__name__)
async def async_print_message(request):
json = await request.json()
print(json.get("message"))
return {"status": "success"}, 200
async def async_log_message(request):
json = await request.json()
logger.warning(json.get("message"))
return {"status": "success"}, 200
async def async_function(request):
return {"execution_id": request.headers.get("Function-Execution-Id")}
async def async_error(request):
return 1 / 0
async def async_sleep(request):
json = await request.json()
message = json.get("message")
logger.warning(message)
await asyncio.sleep(1)
logger.warning(message)
return {"status": "success"}, 200
async def async_trace_test(request):
context = execution_id._get_current_context()
return {
"execution_id": context.execution_id if context else None,
"span_id": context.span_id if context else None,
}
def sync_function_in_async_context(request):
return {
"execution_id": request.headers.get("Function-Execution-Id"),
"type": "sync",
}
def sync_cloudevent_with_context(cloud_event):
context = execution_id._get_current_context()
if context:
logger.warning(f"Execution ID in sync CloudEvent: {context.execution_id}")
else:
logger.error("No execution context in sync CloudEvent function!")
async def async_cloudevent_error(cloudevent):
raise ValueError("This is a test error")