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] Provide a simpler method to run an app locally #690

Closed
sk- opened this issue Dec 17, 2015 · 7 comments
Closed

[Feature Request] Provide a simpler method to run an app locally #690

sk- opened this issue Dec 17, 2015 · 7 comments
Labels

Comments

@sk-
Copy link

sk- commented Dec 17, 2015

Right now, there are several examples that run an app locally in sightly different ways (see tests/autobahn/server.py, examples/web_srv.py, examples/web_ws.py, http://aiohttp.readthedocs.org/en/stable/#getting-started and http://aiohttp.readthedocs.org/en/stable/web.html#run-a-simple-web-server). And if you want to start testing your app, you need to copy that (boilerplate) code snippet.

I propose adding a utility function, or a method to run and app locally in a simpler way.

Right now my utility function is as follows:

def run_app(app, host='0.0.0.0', port=8080):
    """Run an app locally."""
    loop = asyncio.get_event_loop()
    handler = app.make_handler()
    f = loop.create_server(handler, host, port)
    srv = loop.run_until_complete(f)

    if host == '0.0.0.0':
        host = '127.0.0.1'
    print(' * Running on http://{host}:{port}/ (Press CTRL+C to quit)'.format(
        host=host, port=port))

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        loop.run_until_complete(handler.finish_connections(1.0))
        srv.close()
        loop.run_until_complete(srv.wait_closed())
        loop.run_until_complete(app.finish())
    loop.close()

alternatively, this function could be a method of an app.

@fafhrd91
Copy link
Member

+1 for util function, also aiohttp package can define console script

On Dec 17, 2015, at 8:10 AM, Sebastian Kreft notifications@github.com wrote:

Right now, there are several examples that run an app locally in sightly different ways (see tests/autobahn/server.py, examples/web_srv.py, examples/web_ws.py, http://aiohttp.readthedocs.org/en/stable/#getting-started and http://aiohttp.readthedocs.org/en/stable/web.html#run-a-simple-web-server). And if you want to start testing your app, you need to copy that (boilerplate) code snippet.

I propose adding a utility function, or a method to run and app locally in a simpler way.

Right now my utility function is as follows:

def run_app(app, host='0.0.0.0', port=8080):
"""Run an app locally."""
loop = asyncio.get_event_loop()
handler = app.make_handler()
f = loop.create_server(handler, host, port)
srv = loop.run_until_complete(f)

if host == '0.0.0.0':
    host = '127.0.0.1'
print(' * Running on http://{host}:{port}/ (Press CTRL+C to quit)'.format(
    host=host, port=port))

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    loop.run_until_complete(handler.finish_connections(1.0))
    srv.close()
    loop.run_until_complete(srv.wait_closed())
    loop.run_until_complete(app.finish())
loop.close()

alternatively, this function could be a method of an app.


Reply to this email directly or view it on GitHub.

@asvetlov
Copy link
Member

What console script should do?

@fafhrd91
Copy link
Member

something like aiohttp_srv -h 0.0.0.0 -p 8080 my_project.main

@jashandeep-sohi
Copy link
Contributor

+1 for CLI, but is it a good idea to make aiohttp.web itself runnable?
python -m aiohttp.web HOST PORT pkg.module.main [ARGS]

@fafhrd91
Copy link
Member

+1 for aiohttp.web

@asvetlov
Copy link
Member

Fixed by #734

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants