aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
14.95k stars 1.99k forks source link

[aiohttp.web] TypeError: Application.__call__() takes 1 positional argument but 2 were given #7017

Closed 4n1qz5skwv closed 1 week ago

4n1qz5skwv commented 1 year ago

Describe the bug

Trying use "aiohttp" with "nginx unit"

aiohttp is not listed in https://unit.nginx.org/howto/#frameworks

So, tried to use basic ASGI approach. Not working.

To Reproduce

  1. a simple run.py
    
    from aiohttp import web

app = web.Application()


2. configure unit file. a sample of application section,
```json
  "applications": {
    "fooapp": {
      "type": "python 3.10",
      "home": "/path/to/.venv/",
      "path": "/path/to/run.py/folder/",
      "protocol": "asgi",
      "module": "run",
      "callable": "app"
    }
  }
  1. put it online.
  2. visit url.

Expected behavior

Expected behavior:

404: Not Found

Actual behavior:

Error 503.

Logs/tracebacks

/var/log/unit.log (https://unit.nginx.org/installation/#ubuntu)

2022/10/13 12:30:14 [error] 573242#573242 [unit] #3193: Python failed to call legacy app stage1
TypeError: Application.__call__() takes 1 positional argument but 2 were given

Python Version

$ python --version
3.10

aiohttp Version

$ python -m pip show aiohttp
aiohttp==3.8.3

multidict Version

$ python -m pip show multidict

yarl Version

$ python -m pip show yarl

OS

ubuntu 22.04

Related component

Server

Additional context

Please add support for unit.

https://unit.nginx.org/ https://unit.nginx.org/howto/#frameworks

If possible please add this library to unit frameworks list with a demo. It will be very very helpful for us to get started.

Thank you for this awesome lib.

Code of Conduct

4n1qz5skwv commented 1 year ago

If this library added to unit frameworks it will be at top of the list (alphabetical order) 😃

Dreamsorcerer commented 1 year ago

I'm not too familiar with exactly how WSGI works, but our docs suggest either deploying with a proxy over unix socket (https://docs.aiohttp.org/en/stable/deployment.html#nginx-supervisord) or with Gunicorn (https://docs.aiohttp.org/en/stable/deployment.html#nginx-gunicorn).

I think the gunicorn worker creates a WSGI interface (atleast I see references to wsgi in the code: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/worker.py), so maybe you just need to make use of Gunicorn in order to give Unit a WSGI environment to work with?

If you figure it out, we can consider taking any changes or updating the docs if it's not obvious.

To be clear though, the web.Application object does not handle running the program, so your example will definitely not work, you need some kind of runner, e.g. a regular application run would use: https://docs.aiohttp.org/en/stable/web_reference.html#aiohttp.web.run_app or https://docs.aiohttp.org/en/stable/web_advanced.html#application-runners).

4n1qz5skwv commented 1 year ago

WSGI

ASGI ?

4n1qz5skwv commented 1 year ago

WSGI

I tried with protocol "asgi" (https://unit.nginx.org/configuration/#python) .

so your example will definitely not work

I know. But all the popular lib already supported and listed there. I don't know how the unit works actually. It just works with all the similar libraries. So i thought may be it support aiohttp too.

If you can please consider adding support for unit 🙏

@Dreamsorcerer

Dreamsorcerer commented 1 year ago

WSGI

ASGI ?

Seem to be similar, Unit says it supports both, and I suspect you can get Gunicorn to do either too.

I know. But all the popular lib already supported and listed there. I don't know how the unit works actually. It just works with all the similar libraries. So i thought may be it support aiohttp too.

Docs say it just uses WSGI or ASGI (https://unit.nginx.org/#supported-app-languages), so I doubt we need Unit-specific config. But, other libraries often use these as the default method of running their applications, whereas we default to running on a socket. So, you'll just need play around to figure it out. Again, Gunicorn may provide what you need, so I'd start looking there.

bmwant commented 1 year ago

@4n1qz5skwv aiohttp doesn't support ASGI, so you would not be able to run it as described unless you provide your own wrapper around.

See https://github.com/aio-libs/aiohttp/issues/2902

Dreamsorcerer commented 1 week ago

I'm going to close this as a duplicate of #8473, as it doesn't look like there's any other approach that Unit will work other than ASGI.