如何在Python中创建异步生成器?

2024-04-25 05:13:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将Python2.7代码重写为新的异步世界顺序:

def get_api_results(func, iterable):
    pool = multiprocessing.Pool(5)
    for res in pool.map(func, iterable):
        yield res

map()阻塞,直到计算完所有结果,所以我试图将其重写为一个异步实现,该实现将在结果就绪后立即生成结果。与map()一样,返回值的顺序必须与iterable相同。我试过这个(我需要requests,因为旧的身份验证要求):

import requests

def get(i):
    r = requests.get('https://example.com/api/items/%s' % i)
    return i, r.json()

async def get_api_results():
    loop = asyncio.get_event_loop()
    futures = []
    for n in range(1, 11):
        futures.append(loop.run_in_executor(None, get, n))
    async for f in futures:
        k, v = await f
        yield k, v

for r in get_api_results():
    print(r)

但在Python3.6中,我得到了:

  File "scratch.py", line 16, in <module>
    for r in get_api_results():
TypeError: 'async_generator' object is not iterable

我怎样才能做到这一点?


Tags: inloopapimapforgetasync顺序
2条回答

关于旧的(2.7)代码,多处理被认为是一种强大的替代方法,它可以取代更简单的线程模块,用于并发处理CPU密集型任务,在这些任务中,线程工作得不太好。您的代码可能不受CPU限制—因为它只需要发出HTTP请求—线程可能已经足够解决您的问题了。

然而,Python 3+没有直接使用threading,而是有一个很好的模块,名为concurrent.futures,它通过coolExecutor类使用更干净的API。此模块也可作为external package用于python 2.7。

以下代码适用于Python2和Python3:

# For python 2, first run:
#
#    pip install futures
#
from __future__ import print_function

import requests
from concurrent import futures

URLS = [
    'http://httpbin.org/delay/1',
    'http://httpbin.org/delay/3',
    'http://httpbin.org/delay/6',
    'http://www.foxnews.com/',
    'http://www.cnn.com/',
    'http://europe.wsj.com/',
    'http://www.bbc.co.uk/',
    'http://some-made-up-domain.coooom/',
]


def fetch(url):
    r = requests.get(url)
    r.raise_for_status()
    return r.content


def fetch_all(urls):
    with futures.ThreadPoolExecutor(max_workers=5) as executor:
        future_to_url = {executor.submit(fetch, url): url for url in urls}
        print("All URLs submitted.")
        for future in futures.as_completed(future_to_url):
            url = future_to_url[future]
            if future.exception() is None:
                yield url, future.result()
            else:
                # print('%r generated an exception: %s' % (
                # url, future.exception()))
                yield url, None


for url, s in fetch_all(URLS):
    status = "{:,.0f} bytes".format(len(s)) if s is not None else "Failed"
    print('{}: {}'.format(url, status))

此代码基于线程使用futures.ThreadPoolExecutor。这里使用了很多魔法。

上面的python 3.6代码使用^{}来创建一个futures.ProcessPoolExecutor(),而不是真正使用异步IO!!

如果您真的想继续使用asyncio,则需要使用支持asyncio的HTTP客户端,例如aiohttp。下面是一个示例代码:

import asyncio

import aiohttp


async def fetch(session, url):
    print("Getting {}...".format(url))
    async with session.get(url) as resp:
        text = await resp.text()
    return "{}: Got {} bytes".format(url, len(text))


async def fetch_all():
    async with aiohttp.ClientSession() as session:
        tasks = [fetch(session, "http://httpbin.org/delay/{}".format(delay))
                 for delay in (1, 1, 2, 3, 3)]
        for task in asyncio.as_completed(tasks):
            print(await task)
    return "Done."


loop = asyncio.get_event_loop()
resp = loop.run_until_complete(fetch_all())
print(resp)
loop.close()

如您所见,asyncio还有一个as_completed(),现在使用真正的异步IO,在一个进程上只使用一个线程。

你把你的事件循环放在另一个共同程序中。别那么做。事件循环是异步代码的最外层“驱动程序”,应该同步运行。

如果需要处理获取的结果,请编写更多这样做的协程。他们可以从队列中获取数据,也可以直接驱动获取。

您可以有一个获取和处理结果的主函数,例如:

async def main(loop): 
    for n in range(1, 11):
        future = loop.run_in_executor(None, get, n)
        k, v = await future
        # do something with the result

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

我会使用类似于^{}的异步库使get()函数正确地异步,这样您就不必使用执行器了。

相关问题 更多 >