处理ASGI生命周期的中间件
项目描述
asgi-lifespan-middleware
使用简单的异步上下文管理器接口支持ASGI生命周期的ASGI中间件。
此中间件接受一个要包装的ASGI应用程序和一个异步上下文管理器生命周期。它将运行直接传递给它的生命周期以及ASGI应用程序的生命周期(如果包装的ASGI应用程序支持生命周期)。
示例(Starlette)
Starlette应用程序已经支持生命周期,所以我们只需使用TestClient对无操作的普通ASGI应用程序进行测试。
from contextlib import asynccontextmanager
from typing import AsyncIterator
from starlette.testclient import TestClient
from starlette.types import ASGIApp, Scope, Send, Receive
from asgi_lifespan_middleware import LifespanMiddleware
@asynccontextmanager
async def lifespan(
    # you'll get the wrapped app injected
    app: ASGIApp,
) -> AsyncIterator[None]:
    print("setup")
    yield
    print("teardown")
async def app(scope: Scope, receive: Receive, send: Send) -> None:
    ...  # do nothing
wrapped_app = LifespanMiddleware(
    app,
    lifespan=lifespan,
)
with TestClient(wrapped_app):
    pass
          项目详情
    
       关闭
    
      
        
    
    
  
asgi_lifespan_middleware-0.1.3.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 88adb5932e90e47f8a1ac075ce974c39e39def90b99edb43801732102ae5d132 | |
| MD5 | ba3c83d4ea32e318f1adc2a63326fd59 | |
| BLAKE2b-256 | af14e413ef0ed1878a6c84296dd9103653eb9a838a6196acb98d21ec52b059ce | 
    
       关闭
    
      
        
    
    
  
asgi_lifespan_middleware-0.1.3-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | c36734c0bc33f416621ed050c092391b89f0c312b462976d7536e2be941d780d | |
| MD5 | aab411bc11b826e56eae4428d159fdc1 | |
| BLAKE2b-256 | 650c8776c4453a002bf9476c5aafac520c14c5046288190ff34b58f68857138c |