我想知道这是不是在AIOHTTP做这件事的正确方法
您的代码相当惯用。在顶层,你可以忽略
asyncio.ensure_future
简单的呼叫
asyncio.run(run(possible_answers))
.
当条件(状态代码)满足时,如何取消处理?
您可以使用事件或将来的对象并等待它,而不是使用
gather
. 你可能知道,
聚集
不需要
运行
协同程序(它们按计划运行
create_task
,它的明确目的是等待所有协程完成。这个
Event
-基于同步的情况如下:
async def bound_fetch(sem, session, answer, done):
# generating url, headers and json ...
async with sem, session.post(url=url, json=json, headers=headers) as response:
if response.status == 200:
done.set()
done.run_answer = json['answer']
async def run(words):
sem = asyncio.Semaphore(3)
done = asyncio.Event()
async with aiohttp.ClientSession() as session:
tasks = []
for word in words:
tasks.append(asyncio.create_task(bound_fetch(
sem=sem, session=session, answer=''.join(word), done=done)))
print("Generated %d possible answers. Checking %s" % (len(words), base_url))
await done.wait()
print('Right answer found: %s' % done.run_answer)
for t in tasks:
t.cancel()