Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/web/async.py: "no current event loop ..." right after start #25

Open
otaku42 opened this issue May 19, 2023 · 1 comment
Open
Labels
bug Something isn't working

Comments

@otaku42
Copy link
Owner

otaku42 commented May 19, 2023

When I connect to http://localhost:8000/ right after starting the app with uvicorn async:app, the browser only shows the message Internal Server Error.

The console shows:

(v4l2py-testing) ┌─[✗]─[mrenzmann@parrot]─[~/src/v4l2py-testing/examples/web]
└──╼ $uvicorn async:app
INFO:     Started server process [44996]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
AnyIO worker thread 2023-05-19 22:21:39,288 INFO  v4l2py.device.video0: opening /dev/video0
AnyIO worker thread 2023-05-19 22:21:39,293 INFO  v4l2py.device.video0: opened /dev/video0 (Integrated IR Camera: Integrate)
AnyIO worker thread 2023-05-19 22:21:39,293 INFO  v4l2py.device.video0: closing /dev/video0 (Integrated IR Camera: Integrate)
AnyIO worker thread 2023-05-19 22:21:39,293 INFO  v4l2py.device.video0: closed /dev/video0 (Integrated IR Camera: Integrate)
INFO:     127.0.0.1:60972 - "GET / HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 428, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/fastapi/applications.py", line 276, in __call__
    await super().__call__(scope, receive, send)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/fastapi/routing.py", line 165, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/home/mrenzmann/src/v4l2py-testing/.venv/lib/python3.9/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/home/mrenzmann/src/v4l2py-testing/examples/web/async.py", line 107, in index
    "index.html", {"request": request, "cameras": cameras()}
  File "/home/mrenzmann/src/v4l2py-testing/examples/web/async.py", line 94, in cameras
    cameras[device.index] = Camera(device)
  File "/home/mrenzmann/src/v4l2py-testing/examples/web/async.py", line 39, in __init__
    self.clients: asyncio.Queue = asyncio.Queue()
  File "/usr/lib/python3.9/asyncio/queues.py", line 35, in __init__
    self._loop = events.get_event_loop()
  File "/usr/lib/python3.9/asyncio/events.py", line 642, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'AnyIO worker thread'.

The error goes away when calling http://localhost:8000/camera/0/stream, then heading back to http://localhost:8000/. This can serve as a work-around, but it's still annoying.

The duck found another project with a similar error. There it has been fixed already; check how they fixed it and see if their fix works for us, too.

@otaku42 otaku42 added the bug Something isn't working label May 19, 2023
@otaku42
Copy link
Owner Author

otaku42 commented May 20, 2023

See also here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant