Skip to content

Commit c29a554

Browse files
committed
Update FastAPI Response Streaming example
1 parent aa33234 commit c29a554

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

examples/fastapi-response-streaming-zip/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This example shows how to use Lambda Web Adapter to run a FastAPI application with response streaming via a Function URL.
44

5-
### How does it work?
5+
## How does it work?
66

77
We add Lambda Web Adapter layer to the function and configure wrapper script.
88

@@ -14,7 +14,7 @@ We add Lambda Web Adapter layer to the function and configure wrapper script.
1414

1515
To get more information of Wrapper script, please read Lambda documentation [here](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-modify.html#runtime-wrapper).
1616

17-
This is the resource for Lambda function. The function urls's invoke mode is configured as "RESPONSE_STREAM", and Lambda environment variable "AWS_LWA_INVOKE_MODE" is set to "response_stream".
17+
This is the resource for Lambda function. The function urls's invoke mode is configured as "RESPONSE_STREAM", and Lambda environment variable "AWS_LWA_INVOKE_MODE" is set to "response_stream".
1818

1919
```yaml
2020
FastAPIFunction:
@@ -36,17 +36,17 @@ This is the resource for Lambda function. The function urls's invoke mode is con
3636
InvokeMode: RESPONSE_STREAM
3737
```
3838
39-
### Build and Deploy
39+
## Build and Deploy
4040
4141
Run the following commands to build and deploy the application to lambda.
4242
4343
```bash
4444
sam build --use-container
4545
sam deploy --guided
4646
```
47-
When the deployment completes, take note of FastAPI's Value. It is the API Gateway endpoint URL.
4847

49-
### Verify it works
48+
When the deployment completes, take note of FastAPI's Value. It is the API Gateway endpoint URL.
5049

51-
Open FastAPI's URL in a browser, you should see "This is streaming from Lambda" streams back 10 times.
50+
## Verify it works
5251

52+
Open FastAPI's URL in a browser, you should see "This is streaming from Lambda" streams back character by character.

examples/fastapi-response-streaming-zip/app/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77

88
async def streamer():
9-
for i in range(10):
10-
await asyncio.sleep(1)
11-
yield b"This is streaming from Lambda \n"
9+
message = "This is streaming from Lambda!\n"
10+
for char in message:
11+
yield char
12+
await asyncio.sleep(0.1)
1213

1314

1415
@app.get("/")
1516
async def index():
16-
return StreamingResponse(streamer(), media_type="text/plain; charset=utf-8")
17+
return StreamingResponse(streamer(), media_type="text/html")

0 commit comments

Comments
 (0)