以字符串形式获取aiohttp结果 [英] Get aiohttp results as string

查看:259
本文介绍了以字符串形式获取aiohttp结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python中的async从网站获取数据.作为示例,我使用了以下代码(在一个更好的协同程序示例下):

I'm trying to get data from a website using async in python. As an example I used this code (under A Better Coroutine Example): https://www.blog.pythonlibrary.org/2016/07/26/python-3-an-intro-to-asyncio/

现在这可以正常工作,但是它会将二进制块写入文件中,而我不希望在文件中使用它.我想要直接得到的数据.但是我目前有一个协程对象列表,我无法从中获取数据.

Now this works fine, but it writes the binary chunks to a file and I don't want it in a file. I want the resulting data directly. But I currently have a list of coroutine objects which I can not get the data out of.

代码:

# -*- coding: utf-8 -*-
import aiohttp
import asyncio
import async_timeout

async def fetch(session, url):
    with async_timeout.timeout(10):
        async with session.get(url) as response:
            return await response.text()


async def main(loop, urls):
    async with aiohttp.ClientSession(loop=loop) as session:
        tasks = [fetch(session, url) for url in urls]
        await asyncio.gather(*tasks)
        return tasks

# time normal way of retrieval
if __name__ == '__main__':
    urls = [a list of urls..]

    loop = asyncio.get_event_loop()
    details_async = loop.run_until_complete(main(loop, urls))

谢谢

推荐答案

问题出在main()末尾的return tasks中,该问题不在原始文章中.而不是返回协程对象(一旦传递给asyncio.gather就没有用),您应该返回asyncio.gather返回的元组,其中包含以正确顺序运行协程的结果.例如:

The problem is in return tasks at the end of main(), which is not present in the original article. Instead of returning the coroutine objects (which are not useful once passed to asyncio.gather), you should be returning the tuple returned by asyncio.gather, which contains the results of running the coroutines in correct order. For example:

async def main(loop, urls):
    async with aiohttp.ClientSession(loop=loop) as session:
        tasks = [fetch(session, url) for url in urls]
        results = await asyncio.gather(*tasks)
        return results

现在loop.run_until_complete(main(loop, urls))将以与URL相同的顺序返回一个元组文本.

Now loop.run_until_complete(main(loop, urls)) will return a tuple of texts in the same order as the URLs.

这篇关于以字符串形式获取aiohttp结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆