0

I want to create a function to download from a website asynchronously. I need the results of the download to be joined to input parameters so I can use both the results as well as the parameters after the download.

I currently have the following:

async def download(session, url, var1, var2):
    with async_timeout.timeout(10):
        async with session.get(url) as response:
            return await (response.read(), url, var1, var2)

async def loop_download(loop, urls, var1s, var2s):
    async with aiohttp.ClientSession(loop=loop) as session:
        tasks = [download(session, url, var1, var2) for url, var1, var2 in zip(urls, var1s, var2s)]
        results = await asyncio.gather(*tasks)
        return results

loop = asyncio.get_event_loop()
results = loop.run_until_complete(loop_download(loop, urls, var1s, var2s))

This however returns an error:

TypeError: object tuple can't be used in 'await' expression

How can I join some input data (for example the url) to the results so I can use this for further analyses?

3
  • 3
    return (await response.read(), url, var1, var2) ? Or even better, take the value just the line before Commented Dec 18, 2018 at 13:58
  • That returns TypeError: An asyncio.Future, a coroutine or an awaitable is required Commented Dec 18, 2018 at 14:00
  • The tl;dr here is you can return a tuple but you can't await one. Commented Dec 18, 2018 at 21:13

1 Answer 1

5

Solved it with:

return (await response.read(), url, x, y)
Sign up to request clarification or add additional context in comments.

1 Comment

You can also drop the parentheses: return await response.read(), url, x, y

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.