aioalice icon indicating copy to clipboard operation
aioalice copied to clipboard

Error got Future <Future pending> attached to a different loop

Open mahenzon opened this issue 2 years ago • 1 comments

Example

from aiohttp import web
from aioalice import Dispatcher, get_new_configured_app


WEBHOOK_URL_PATH = '/my-alice-webhook/'  # webhook endpoint

WEBAPP_HOST = 'localhost'
WEBAPP_PORT = 3001

dp = Dispatcher()


@dp.request_handler()
async def handle_all_requests(alice_request):
    return alice_request.response('Привет этому миру!')


if __name__ == '__main__':
    app = get_new_configured_app(dispatcher=dp, path=WEBHOOK_URL_PATH)
    web.run_app(app, host=WEBAPP_HOST, port=WEBAPP_PORT)

Error

======== Running on http://localhost:3001 ========
(Press CTRL+C to quit)
Error handling request
Traceback (most recent call last):
  File "/user/simple-alice/venv/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 433, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/user/simple-alice/venv/lib/python3.11/site-packages/aiohttp/web_app.py", line 504, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/user/simple-alice/venv/lib/python3.11/site-packages/aiohttp/web_urldispatcher.py", line 954, in _iter
    resp = await method()
           ^^^^^^^^^^^^^^
  File "/user/simple-alice/venv/lib/python3.11/site-packages/aioalice/dispatcher/webhook.py", line 183, in post
    result = await self.process_request(request)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/user/simple-alice/venv/lib/python3.11/site-packages/aioalice/dispatcher/webhook.py", line 101, in process_request
    await waiter
RuntimeError: Task <Task pending name='Task-5' coro=<RequestHandler._handle_request() running at /user/simple-alice/venv/lib/python3.11/site-packages/aiohttp/web_protocol.py:433> cb=[Task.task_wakeup()]> got Future <Future pending> attached to a different loop

mahenzon avatar Feb 23 '23 19:02 mahenzon

Вариант решения: передать текущий event loop в web.run_app:

if __name__ == '__main__':
    app = get_new_configured_app(dispatcher=dp, path=WEBHOOK_URL_PATH)
    web.run_app(app, host=WEBAPP_HOST, port=WEBAPP_PORT, loop=dp.loop)

mahenzon avatar Feb 23 '23 19:02 mahenzon