Coder Social home page Coder Social logo

first example crashes about aiohttp-asgi HOT 4 CLOSED

mosquito avatar mosquito commented on June 30, 2024
first example crashes

from aiohttp-asgi.

Comments (4)

jrobbins-LiveData avatar jrobbins-LiveData commented on June 30, 2024

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.

jrobbins-LiveData avatar jrobbins-LiveData commented on June 30, 2024

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.

jrobbins-LiveData avatar jrobbins-LiveData commented on June 30, 2024

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.

mosquito avatar mosquito commented on June 30, 2024

Fixed in 0.5.0

from aiohttp-asgi.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.