コンテンツにスキップ
per page, with , order by , clip by
Results of 1 - 10 of about 16 for await (0.003 sec.)
WebSocket 14070
WebSocket FastAPI で WebSocket が使用できます。 WebSockets のインストール まず WebSockets のインストールが必要です。 $ pip install websockets ---> 100% WebSocket クライアント 本番環境 本番環境では、React、Vue.js、Angularなどの最新のフレーム ... ef get (): return HTMLResponse ( html ) @app . websocket ( "/ws" ) async def websocket_endpoint ( websocket : WebSocket ): await websocket . accept () while True : data = await websocket . receive_text () await websocket . send_text ( f "Message text ... ef get (): return HTMLResponse ( html ) @app . websocket ( "/ws" ) async def websocket_endpoint ( websocket : WebSocket ): await websocket . accept () while True : data = await websocket . receive_text () await websocket . send_text ( f "Message text ... 同じ WebSocket を提供します。しかし、こちらはStarletteから直接提供されるものです。 メッセージの送受信 WebSocketルートでは、 await を使ってメッセージの送受信ができます。 Python 3.8+ from fastapi import FastAPI , WebSocket from fastapi.responses import H ...
https://man.mtool.work/fastapi/0.115/advanced/websockets/
並行処理と async / await 10847
並行処理と async / await path operation 関数 のための async def に関する詳細と非同期 (asynchronous) コード、並行処理 (Concurrency)、そして、並列処理 (Parallelism) の背景について。 急いでいますか? TL;DR: 次のような、 await を使用して呼び出す ... べきサードパーティライブラリを使用している場合: results = await some_library () 以下の様に async def を使用して path operation 関数 を宣言します。 @app . get ( '/' ) async def read_resul ... ts (): results = await some_library () return results 備考 async def を使用して作成された関数の内部でしか await は使用できません。 データベース、 ... API、ファイルシステムなどと通信し、 await の使用をサポートしていないサードパーティライブラリ (現在のほとんどのデータベースライブラリに当てはまります) を使用している ...
https://man.mtool.work/fastapi/0.115/async/
Custom Request and APIRoute class 10050
Custom Request and APIRoute class Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . In some cases, you may want to override the logic used by the Request and APIRout ... mport APIRoute class GzipRequest ( Request ): async def body ( self ) -> bytes : if not hasattr ( self , "_body" ): body = await super () . body () if "gzip" in self . headers . getlist ( "Content-Encoding" ): body = gzip . decompress ( body ) self . ... tom_route_handler ( request : Request ) -> Response : request = GzipRequest ( request . scope , request . receive ) return await original_route_handler ( request ) return custom_route_handler app = FastAPI () app . router . route_class = GzipRoute @ap ... mport APIRoute class GzipRequest ( Request ): async def body ( self ) -> bytes : if not hasattr ( self , "_body" ): body = await super () . body () if "gzip" in self . headers . getlist ( "Content-Encoding" ): body = gzip . decompress ( body ) self . ...
https://man.mtool.work/fastapi/0.115/how-to/custom-request-and-route/
Async Tests 9652
Async Tests Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . You have already seen how to test your FastAPI applications using the provided TestClient . Up to now, ... t_root (): async with AsyncClient ( transport = ASGITransport ( app = app ), base_url = "http://test" ) as ac : response = await ac . get ( "/" ) assert response . status_code == 200 assert response . json () == { "message" : "Tomato" } Run it You can ... t_root (): async with AsyncClient ( transport = ASGITransport ( app = app ), base_url = "http://test" ) as ac : response = await ac . get ( "/" ) assert response . status_code == 200 assert response . json () == { "message" : "Tomato" } Tip Note that ... as before when using the TestClient . Then we can create an AsyncClient with the app, and send async requests to it, using await . Python 3.8+ import pytest from httpx import ASGITransport , AsyncClient from .main import app @pytest . mark . anyio asy ...
https://man.mtool.work/fastapi/0.115/advanced/async-tests/
Testing WebSockets 9253
Testing WebSockets Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . You can use the same TestClient to test WebSockets. For this, you use the TestClient in a with s ... f read_main (): return { "msg" : "Hello World" } @app . websocket ( "/ws" ) async def websocket ( websocket : WebSocket ): await websocket . accept () await websocket . send_json ({ "msg" : "Hello WebSocket" }) await websocket . close () def test_read ...
https://man.mtool.work/fastapi/0.115/advanced/testing-websockets/
ミドルウェア 8023
ミドルウェア FastAPI アプリケーションにミドルウェアを追加できます。 「ミドルウェア」は、すべての リクエスト に対して、それがあらゆる特定の path operation によって処理される前に機能する関数です。また、すべての レスポンス に対して、それを返す ... ttp" ) async def add_process_time_header ( request : Request , call_next ): start_time = time . perf_counter () response = await call_next ( request ) process_time = time . perf_counter () - start_time response . headers [ "X-Process-Time" ] = str ( p ... ttp" ) async def add_process_time_header ( request : Request , call_next ): start_time = time . perf_counter () response = await call_next ( request ) process_time = time . perf_counter () - start_time response . headers [ "X-Process-Time" ] = str ( p ...
https://man.mtool.work/fastapi/0.115/tutorial/middleware/
Request Files 8023
Request Files Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . You can define files to be uploaded by the client using File . Info To receive uploaded files, first ... eads size ( int ) bytes/characters of the file. seek(offset) : Goes to the byte position offset ( int ) in the file. E.g., await myfile.seek(0) would go to the start of the file. This is especially useful if you run await myfile.read() once and then n ... eed to read the contents again. close() : Closes the file. As all these methods are async methods, you need to "await" them. For example, inside of an async path operation function you can get the contents with: contents = await myfile . re ... . file . read () async Technical Details When you use the async methods, FastAPI runs the file methods in a threadpool and awaits for them. Starlette Technical Details FastAPI 's UploadFile inherits directly from Starlette 's UploadFile , but adds som ...
https://man.mtool.work/fastapi/0.115/tutorial/request-files/
Lifespan Events 7815
Lifespan Events Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . You can define logic (code) that should be executed before the application starts up . This means t ... ersions of Python, there's also an async context manager . You would use it with async with : async with lifespan ( app ): await do_stuff () When you create a context manager or an async context manager like above, what it does is that, before enterin ... it involves I/O (input/output), that requires "waiting" for things to be written to disk. But open() doesn't use async and await . So, we declare the event handler function with standard def instead of async def . startup and shutdown together There's ...
https://man.mtool.work/fastapi/0.115/advanced/events/
Generate Clients 7728
Generate Clients Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . As FastAPI is based on the OpenAPI specification, you get automatic compatibility with many tools, ... n . dumps ( openapi_content )) import * as fs from 'fs' async function modifyOpenAPIFile ( filePath ) { try { const data = await fs . promises . readFile ( filePath ) const openapiContent = JSON . parse ( data ) const paths = openapiContent . paths fo ... )) { const newOperationId = operationId . substring ( toRemove . length ) operation . operationId = newOperationId } } } } await fs . promises . writeFile ( filePath , JSON . stringify ( openapiContent , null , 2 ), ) console . log ( 'File successfull ...
https://man.mtool.work/fastapi/0.115/advanced/generate-clients/
Using Dataclasses 7676
Using Dataclasses Warning The current page still doesn't have a translation for this language. But you can help translating it: Contributing . FastAPI is built on top of Pydantic , and I have been showing you how to use Pydantic models to d ... s needed. If you need a refresher about when to use which, check out the section "In a hurry?" in the docs about async and await . This path operation function is not returning dataclasses (although it could), but a list of dictionaries with internal ...
https://man.mtool.work/fastapi/0.115/advanced/dataclasses/
PREV 1 2 NEXT