Comments (4)
Changing this code (startup=True
) to (startup=False
) prevents the crash.
This crashes
# [Optional]
asgi_resource.lifespan_mount(
aiohttp_app,
startup=True,
# By default starlette didn't
# handle "lifespan.shutdown"
shutdown=False,
)
This does not crash, but presumably then doesn't do anything useful?
# [Optional]
asgi_resource.lifespan_mount(
aiohttp_app,
startup=False,
# By default starlette didn't
# handle "lifespan.shutdown"
shutdown=False,
)
from aiohttp-asgi.
The issue appears to be due to the fact that lifespan_mount
instances an asyncio Queue
before aiohttp
has created the asyncio event loop.
def lifespan_mount(self, app: Application, startup=True, shutdown=False):
receives = asyncio.Queue() # type: asyncio.Queue
Since there is no event loop, asyncio.Queue()
creates one(!)
def __init__(self, maxsize=0, *, loop=None):
if loop is None:
self._loop = events.get_event_loop()
Which is not what aiohttp expects in its web.run_app()
.
from aiohttp-asgi.
Using a coroutine to web.run_app()
seems to fix the problem. See this doc which states
The coroutine based approach allows to perform async IO before making an app.
from aiohttp import web
from fastapi import FastAPI
from starlette.requests import Request as ASGIRequest
from aiohttp_asgi import ASGIResource
asgi_app = FastAPI()
@asgi_app.get("/asgi")
async def root(request: ASGIRequest):
return {
"message": "Hello World",
"root_path": request.scope.get("root_path")
}
async def app_factory():
aiohttp_app = web.Application()
# Create ASGIResource which handle
# any request startswith "/asgi"
asgi_resource = ASGIResource(
asgi_app,
root_path="/asgi"
)
# Register resource
aiohttp_app.router.register_resource(
asgi_resource
)
# [Optional]
asgi_resource.lifespan_mount(
aiohttp_app,
startup=True,
# By default starlette didn't
# handle "lifespan.shutdown"
shutdown=False,
)
return aiohttp_app
# Start the application
web.run_app(app_factory())
from aiohttp-asgi.
Fixed in 0.5.0
from aiohttp-asgi.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiohttp-asgi.