Yorian
Yorian

Reputation: 2062

python async await can not return tuple

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?

Upvotes: 0

Views: 6006

Answers (1)

Yorian
Yorian

Reputation: 2062

Solved it with:

return (await response.read(), url, x, y)

Upvotes: 5

Related Questions