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

Example code from client quickstart not working #3376

Closed
caliph007 opened this issue Nov 1, 2018 · 6 comments
Closed

Example code from client quickstart not working #3376

caliph007 opened this issue Nov 1, 2018 · 6 comments
Labels

Comments

@caliph007
Copy link

I hope I am not fully making a fool of myself.
I am not able to get to work the few lines of the client quickstart code:

import aiohttp
async with aiohttp.ClientSession() as session:
    async with session.get('http://httpbin.org/get') as resp:
        print(resp.status)
        print(await resp.text())

in Python 3.6. I get:

async with aiohttp.ClientSession() as session:
SyntaxError: invalid syntax

That I understand, because async became a reserved work in 3.7.
But the doc states as dependency Python 3.5.3+

in Python 3.7 I get:

async with aiohttp.ClientSession() as session:
SyntaxError: 'async with' outside async function

What am I doing wrong?
Thank you in advance.

@aio-libs-bot
Copy link

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #3175 (aiohttp client timeout does not work), #1309 (Client Tracing), #556 (Example in readme doesn't work as is), #1640 ( Readme client example throws an AssertionError), and #3234 (NameError during WebSockets example in Web Server quickstart docs).

@levkovskyi
Copy link

@caliph007 You can't use async code without loop.

Full example of code in Python 3.5-3.6:

import asyncio
import aiohttp


async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get('http://httpbin.org/get') as resp:
            print(resp.status)
            print(await resp.text())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Full example of code in Python 3.7

import asyncio
import aiohttp


async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get('http://httpbin.org/get') as resp:
            print(resp.status)
            print(await resp.text())

asyncio.run(main())

@asvetlov
Copy link
Member

asvetlov commented Nov 2, 2018

await can be used inside async def function only.
This is not an aiohttp issue at all

@asvetlov asvetlov closed this as completed Nov 2, 2018
@UtahDave
Copy link

UtahDave commented Dec 7, 2018

I think this issue should be reopened. The client quickstart https://docs.aiohttp.org/en/stable/client_quickstart.html has examples that don't work, as @caliph007 noted above

@asvetlov
Copy link
Member

asvetlov commented Dec 8, 2018

Please feel free to open a Pull Request with docs update

@lock
Copy link

lock bot commented Dec 8, 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 Dec 8, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Dec 8, 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

5 participants