【发布时间】:2021-10-23 07:58:24
【问题描述】:
我想将一些处理程序方法放入字典并仅调用其中一些(基于所需的处理程序)。然而,当 asyncio.gather() 被执行时,所有的任务都会被执行。 这是代码sn-p:
import asyncio
class DataHandler():
def __init__(self):
pass
async def generate_coroutines(self):
all_handlers = {
'handler1': asyncio.create_task(self.handler1()),
'handler2': asyncio.create_task(self.handler2()),
'handler3': asyncio.create_task(self.handler3()),
}
return all_handlers
async def main(self, handlers):
print('Main method started')
all_handlers = await self.generate_coroutines()
print('Handler coroutines created')
# Only add the handlers that has been given as the argument
required_handlers = []
for handler in handlers:
if handler in all_handlers.keys(): required_handlers.append(all_handlers[handler])
output = list(await asyncio.gather(*required_handlers))
print(output)
async def handler1(self):
print('handler1 executed')
return 'handler1_output'
async def handler2(self):
print('handler2 executed')
return 'handler2_output'
async def handler3(self):
print('handler3 executed')
return 'handler3_output'
if __name__ == '__main__':
dh = DataHandler()
loop = asyncio.get_event_loop()
loop.run_until_complete( dh.main(['handler2']))
输出:
Main method started
Handler coroutines created
handler1 executed
handler2 executed
handler3 executed
['handler2_output']
期望的输出:
Main method started
Handler coroutines created
handler2 executed
['handler2_output']
如果我取消未使用的任务,则它们不会被执行。但是是不是可以创建所有可能的任务,只执行其中一些,让其他的走(不需要取消其余的)
【问题讨论】:
标签: python python-3.x asynchronous async-await python-asyncio