2

我有一段 I/O 绑定代码,基本上是为我的一个研究项目做一些网络抓取。

代码开始是命令式的,然后变成了列表理解,现在主要变成了生成器:

if __name__ == '__main__':
    while True:
        with suppress(Exception):
            page = requests.get(baseUrl).content
        urls = (baseUrl + link['href'] for link in BeautifulSoup(page,'html.parser').select('.tournament a'))
        resources = (scrape_host(url) for url in urls)
        keywords = ((keywords_for_resource(referer, site_id), rid) for
                          referer, site_id, rid in resources)
        output = (scrape(years, animals) for years, animals in keywords)
        responses = (post_data_with_exception_handling(list(data)) for data in output)
        for response in responses:
            print(response.status_code)

这种代码真的很适合我,因为它基于生成器,不需要存储太多状态,我想我可以很容易地将它变成asyncio基于代码:

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):
    async with aiohttp.ClientSession(loop=loop) as session: 
        page = await fetch(session,baseUrl)
        urls = (baseUrl + link['href'] for link in BeautifulSoup(page,'html.parser').select('.tournament a'))
        subpages = (await fetch(session,url) for url in urls)

然而,在 Python 3.5 中,这只是返回 a Syntax error,因为await表达式不允许在推导中使用。

Python 3.6 承诺在 pep 530 中实现异步生成器

此功能是否使我能够asyncio轻松地将基于生成器的代码转换为代码,还是还需要完全重写?

4

1 回答 1

0

asyncio.as_completed()这里可能是一个更好的解决方案:

# pip install beautifulsoup4 aiohttp
import asyncio
from urllib.parse import urljoin

import aiohttp
import async_timeout
from bs4 import BeautifulSoup

BASE_URL = "http://www.thewebsiteyouarescraping.com/"
SELECTOR = ".tournament a"

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


async def main(base_url, selector, loop):
    async with aiohttp.ClientSession(loop=loop) as session:
        _, page = await fetch(session, base_url)
        urls = (urljoin(base_url, link['href']) for link in
                BeautifulSoup(page, 'html.parser').select(selector))
        tasks = {fetch(session, url): url for url in urls}
        for fut in asyncio.as_completed(tasks, loop=loop):
            process(*await fut)
        # Compare with:
        # for fut in tasks:
        #     process(*await fut)


def process(url, page):
    print(url, len(page))


loop = asyncio.get_event_loop()
loop.run_until_complete(main(BASE_URL, SELECTOR, loop))
loop.close()
于 2016-12-25T01:21:49.120 回答