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

[Feature Request] Uvicorn integration #204

Closed
alimcmaster1 opened this issue Oct 4, 2019 · 7 comments
Closed

[Feature Request] Uvicorn integration #204

alimcmaster1 opened this issue Oct 4, 2019 · 7 comments
Labels
enhancement Enhanvement request help wanted Community help is wanted

Comments

@alimcmaster1
Copy link

🚀 Feature Request

Firstly thank you for open sourcing this - documentation is excellent too!.

Motivation

Often ASGI/WSGI python webserver frameworks are launched with their own wrapper module such as gunicorn, uvicorn and hypercorn.

Example as per the uvicorn quickstart guide.
(note the run command: uvicorn example:app)

The uvicorn command then runs this main module.

If one wishes to use hydra for configuration management in the uvicorn examples they would need to rewrite some code from the above module perhaps:

import hydra
from uvicorn.supervisors import Multiprocess
from uvicorn.config import Config
from uvicorn.main import Server


async def app(scope, receive, send):
    assert scope['type'] == 'http'
    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            [b'content-type', b'text/plain'],
        ]
    })
    await send({
        'type': 'http.response.body',
        'body': b'Hello, world!',
    })


@hydra.main()
def run(cfg):
    print(cfg.pretty())

    config = Config(app)
    server = Server(config=config)
    sock = config.bind_socket()
    supervisor = Multiprocess(config)
    supervisor.run(server.run, sockets=[sock])


if __name__ == "__main__":
    run()

Pitch

Describe the solution you'd like

A very open ended question but what are peoples thoughts on developing a way to make these libraries work together

Goals:

  • users don't have to duplicate code.
  • make it easier to pass the hydra configuration into the uvicorn application

Are you willing to open a pull request?

  • Yes

cc. @tomchristie

@alimcmaster1 alimcmaster1 added the enhancement Enhanvement request label Oct 4, 2019
@omry omry changed the title [Feature Request] [Feature Request] Univorn integration Oct 4, 2019
@omry omry changed the title [Feature Request] Univorn integration [Feature Request] Uvicorn integration Oct 4, 2019
@omry
Copy link
Collaborator

omry commented Oct 4, 2019

Hi @alimcmaster1.
Thanks for the feature request, and I appreciate your kind words!
I am not a web developer (any longer), and I was not aware of those frameworks (although I did use WSGI as a user at some point).

The execution model is one of the areas where Hydra is not very flexible right now.
I will want to take a closer look at Uvicorn to see what a nice integration might look like.
On the surface this sounds like more a feature request for Uvicorn than to Hydra (To allow accepting an external configuration).

I suggest that you try using Hydra with Uvicorn for a while (even if the integration is not clean) to see what works well and what doesn't work well before we start thinking about what needed to be changed in Hydra to support this better.

@omry
Copy link
Collaborator

omry commented Oct 4, 2019

Generally speaking, Hydra is lower level than traditional frameworks.
Most frameworks in ML (I am not talking about Uvicorn here), are provide - apart from the value in their actual domain - a solution for a common set of problems (Command line interface, configuration files, remote launching, logging etc).
For such frameworks, it's best if they are modified to use Hydra as a base because it can solve all those problems for them.
The extra advantage here is that after doing that, suddenly they can easily be integrated into a Hydra app that may be using some other such frameworks at the same time. something like this would normally be very painful but with Hydra it's a breeze.

I do not have a good mental model of Uvicorn right now.
From the little I see, it seems that they went the way of uvicorn python_mdule as a launching mechanism. this is pretty hard to integrate with for something like Hydra.

Beyond configuring itself, does Uvicorn cocerns itself at all with how the user's web server would configure itself?
I saw that Uvicorn has a simple configuration object.
If this is the extent to which Uvicorn is concerning itself with configuration, it may be pretty straight forward to change (Or wrap) it in a way that takes advantage of Hydra more naturally.

In your example, what I am not seeing is how you use Hydra's config to configure Uvicorn and how you are using it to configure the behavior of the actual app.

@omry omry added the help wanted Community help is wanted label Oct 19, 2019
@omry
Copy link
Collaborator

omry commented Nov 7, 2019

@hydra.compose API that is planned in #219 might be helpful here.

@omry
Copy link
Collaborator

omry commented Nov 8, 2019

directus-api looks like a pretty solid example of an integration between Hydra and Uvicorn.

https://github.com/devova/directus-api/

@alimcmaster1
Copy link
Author

@omry - Thanks for your detailed responses - appreciate it.

Yes agree #219 - looks interesting - will keep an eye out and try this when available.

Thanks very much for the example - that's a neat way of integrating uvicorn and hydra!

@omry
Copy link
Collaborator

omry commented Nov 23, 2019

#219 is now landed (and is available in the Hydra 0.11). I ended up with an experimental API to perform composition.
See this (The website is a bit outdated but should be fixed soon).

It would be great if anyone interested in Uvicorn and Hydra contribute a minimal and educational example of how they think is a good way to integrate them.

@omry
Copy link
Collaborator

omry commented Jan 20, 2020

Closing, this should be possible via the compose API or following the example I pointed to.

@omry omry closed this as completed Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhanvement request help wanted Community help is wanted
Projects
None yet
Development

No branches or pull requests

2 participants