Skip to content

Commit

Permalink
add fast api web socker and streaming api
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcodeforce committed Apr 24, 2024
1 parent a652359 commit 8fdc80e
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/python/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,8 @@ A scope is a textual region of a Python program, where a namespace is directly a


???- question "How to program web socket server"
See [FastAPI doc](https://fastapi.tiangolo.com/advanced/websockets/) with [testing](https://fastapi.tiangolo.com/advanced/testing-websockets/) and matching code in [](https://github.com/jbcodeforce/python-code/tree/master/web-server/websocket_server)
See [FastAPI doc](https://fastapi.tiangolo.com/advanced/websockets/) with [testing](https://fastapi.tiangolo.com/advanced/testing-websockets/) and matching code in [websocket_server](https://github.com/jbcodeforce/python-code/tree/master/web_server/websocket_server)

???- question "Using async IO?"
async IO is a single-threaded, single-process design: it uses cooperative multitasking. [See this tutorial](https://realpython.com/async-io-python/): concurrency encompasses both multiprocessing (ideal for CPU-bound tasks) and threading (suited for IO-bound tasks). Async io is supported by `async` and `await` language keywords. Asynchronous routines are able to “pause” while waiting on their ultimate result and let other routines run in the meantime. `await` passes function control back to the event loop
See code in [api_stream](https://github.com/jbcodeforce/python-code/tree/master/web_server/api_stream/)
2 changes: 2 additions & 0 deletions docs/python/pydantic.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Pydantic

[]()

File renamed without changes.
6 changes: 6 additions & 0 deletions web_server/api_stream/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import requests


with requests.get("http://localhost:8000", stream=True) as r:
for chunk in r.iter_content(100):
print(chunk)
21 changes: 21 additions & 0 deletions web_server/api_stream/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fastapi import FastAPI
from typing import Generator
from fastapi.responses import StreamingResponse

app = FastAPI()

# coroutine which will suspend the execution if there is await or yield
# yield creates an asynchronous generator, which we iterate over with async for.
async def fake_video_streamer():
for i in range(10):
yield b"some fake video bytes"

# another example
# A simple method to open the file and get the data
async def get_data_from_file(file_path: str) -> Generator:
with open(file=file_path, mode="rb") as file_like:
yield file_like.read()

@app.get("/")
async def main():
return StreamingResponse(content=fake_video_streamer(),media_type="text/media")
1 change: 1 addition & 0 deletions web_server/api_stream/start_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uvicorn server:app --port 8000
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8fdc80e

Please sign in to comment.