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

if __name__ == '__main__' for main examples. #3775

Closed
samuelcolvin opened this issue May 16, 2019 · 4 comments · Fixed by #3788

Comments

@samuelcolvin
Copy link
Member

commented May 16, 2019

aio-libs/aiohttp-devtools#229

Would be useful if we wrapped web.run_app(app) if a main check:

if __name__ == '__main__':
    web.run_app(app)

To work with aiohttp-devtools and other such tools.

If you're happy with this I'll submit a PR.

@asvetlov

This comment has been minimized.

Copy link
Member

commented May 16, 2019

I quite don't follow what exact code should be wrapped?
Is it part of documentation?

@samuelcolvin

This comment has been minimized.

Copy link
Member Author

commented May 16, 2019

sorry, yes in the documentation the server example should become:

from aiohttp import web

async def handle(request):
    name = request.match_info.get('name', "Anonymous")
    text = "Hello, " + name
    return web.Response(text=text)

app = web.Application()
app.add_routes([web.get('/', handle),
                web.get('/{name}', handle)])

if __name__ == '__main__':
    web.run_app(app)

So that importing the module doesn't run the server.

here

@asvetlov

This comment has been minimized.

Copy link
Member

commented May 16, 2019

I'm fine with the proposal

@samuelcolvin

This comment has been minimized.

Copy link
Member Author

commented May 16, 2019

will do next week and fix my other PR.

NewUserHa added a commit to NewUserHa/aiohttp-devtools that referenced this issue May 16, 2019
samuelcolvin added a commit to aio-libs/aiohttp-devtools that referenced this issue May 17, 2019
update readme to aviod misuse (#230)
*  aio-libs/aiohttp#3775

* Update README.rst

* fix linting
samuelcolvin added a commit to samuelcolvin/aiohttp that referenced this issue May 21, 2019

@helpr helpr bot added the pr-available label May 21, 2019

@helpr helpr bot removed the pr-available label May 21, 2019

asvetlov added a commit that referenced this issue May 21, 2019

@helpr helpr bot added the pr-merged label May 21, 2019

asvetlov added a commit that referenced this issue May 21, 2019
[3.5] if __name__ == '__main__', fix #3775 (#3788)
(cherry picked from commit 506c244)

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
asvetlov added a commit that referenced this issue May 22, 2019
[3.5] if __name__ == '__main__', fix #3775 (#3788) (#3789)
(cherry picked from commit 506c244)

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.