11

我编写了一个脚本,它使用一个 Nursery 和 asks 模块来循环并根据循环变量调用一个 API。我收到回复,但不知道如何像使用 asyncio 一样返回数据。

我还有一个关于将 API 限制为每秒 5 个的问题。

from datetime import datetime
import asks
import time
import trio

asks.init("trio")
s = asks.Session(connections=4)

async def main():
    start_time = time.time()

    api_key = 'API-KEY'
    org_id = 'ORG-ID'
    networkIds = ['id1','id2','idn']

    url = 'https://api.meraki.com/api/v0/networks/{0}/airMarshal?timespan=3600'
    headers = {'X-Cisco-Meraki-API-Key': api_key, 'Content-Type': 'application/json'}

    async with trio.open_nursery() as nursery:
        for i in networkIds:
            nursery.start_soon(fetch, url.format(i), headers)

    print("Total time:", time.time() - start_time)



async def fetch(url, headers):
    print("Start: ", url)
    response = await s.get(url, headers=headers)
    print("Finished: ", url, len(response.content), response.status_code)




if __name__ == "__main__":
    trio.run(main)

当我运行nursery.start_soon(fetch...) 时,我在fetch 中打印数据,但是如何返回数据?我没有看到任何类似于 asyncio.gather(*tasks) 函数的东西。

此外,我可以将会话数限制为 1-4,这有助于低于每秒 5 个 API 的限制,但我想知道是否有内置方法可以确保在任何给定的秒内调用不超过 5 个 API ?

4

4 回答 4

6

返回数据:将 networkID 和 dict 传递给fetch任务:

async def main():
    …
    results = {}
    async with trio.open_nursery() as nursery:
        for i in networkIds:
            nursery.start_soon(fetch, url.format(i), headers, results, i)
    ## results are available here

async def fetch(url, headers, results, i):
    print("Start: ", url)
    response = await s.get(url, headers=headers)
    print("Finished: ", url, len(response.content), response.status_code)
    results[i] = response

或者,创建一个trio.Queueput的结果;然后,您的主要任务可以从队列中读取结果。

API 限制:按照以下方式创建trio.Queue(10)并启动任务:

async def limiter(queue):
    while True:
        await trio.sleep(0.2)
        await queue.put(None)

将该队列作为另一个参数传递给fetch,并await limit_queue.get()在每个 API 调用之前调用。

于 2018-10-05T18:38:48.547 回答
5

从技术上讲,trio.Queue已在 trio 0.9 中弃用。它已被trio.open_memory_channel.

简短的例子:

sender, receiver = trio.open_memory_channel(len(networkIds)
async with trio.open_nursery() as nursery:
    for i in networkIds:
        nursery.start_soon(fetch, sender, url.format(i), headers)

async for value in receiver:
    # Do your job here
    pass

在你的fetch函数中,你应该在async sender.send(value)某个地方调用。

于 2018-12-06T11:10:55.903 回答
4

当我运行nursery.start_soon(fetch...) 时,我在fetch 中打印数据,但是如何返回数据?我没有看到任何类似于 asyncio.gather(*tasks) 函数的东西。

你问了两个不同的问题,所以我只回答这个。马蒂亚斯已经回答了你的另一个问题。

当您调用 时start_soon(),您是在要求 Trio 在后台运行任务,然后继续执行。这就是 Trio 能够同时运行fetch()多次的原因。但是因为 Trio 一直在运行,所以无法像 Python 函数通常那样“返回”结果。它甚至会回到哪里?

您可以使用队列让fetch()任务将结果发送到另一个任务以进行额外处理。

创建队列:

response_queue = trio.Queue()

当您开始获取任务时,将队列作为参数传递,并在完成后向队列发送哨兵:

async with trio.open_nursery() as nursery:
    for i in networkIds:
        nursery.start_soon(fetch, url.format(i), headers)
await response_queue.put(None)

下载 URL 后,将响应放入队列中:

async def fetch(url, headers, response_queue):
    print("Start: ", url)
    response = await s.get(url, headers=headers)
    # Add responses to queue
    await response_queue.put(response)
    print("Finished: ", url, len(response.content), response.status_code)

通过上述更改,您的 fetch 任务会将响应放入队列中。现在您需要从队列中读取响应,以便处理它们。您可以添加一个新函数来执行此操作:

async def process(response_queue):
    async for response in response_queue:
        if response is None:
            break
        # Do whatever processing you want here.

在开始任何 fetch 任务之前,您应该将此流程函数作为后台任务启动,以便在收到响应后立即处理它们。

在 Trio 文档的同步和通信任务部分阅读更多内容。

于 2018-10-05T18:59:43.680 回答
2

基于此答案,您可以定义以下函数:

async def gather(*tasks):

    async def collect(index, task, results):
        task_func, *task_args = task
        results[index] = await task_func(*task_args)

    results = {}
    async with trio.open_nursery() as nursery:
        for index, task in enumerate(tasks):
            nursery.start_soon(collect, index, task, results)
    return [results[i] for i in range(len(tasks))]

然后,您可以通过简单地修补 trio(添加收集功能)以与 asyncio 完全相同的方式使用 trio:

import trio
trio.gather = gather

这是一个实际的例子:

async def child(x):
    print(f"Child sleeping {x}")
    await trio.sleep(x)
    return 2*x

async def parent():
    tasks = [(child, t) for t in range(3)]
    return await trio.gather(*tasks)

print("results:", trio.run(parent))
于 2019-06-05T08:56:29.707 回答