python-3.x - 如何运行长时间运行的任务并立即返回结果?
问题描述
我想运行任务并立即返回结果
tasks = [
asyncio.create_task(self.start_update(update, spec))
for update, spec in zip(updates, specs)
]
return Report(updates=updates,job_task=asyncio.gather(*tasks))
现在我想更改逻辑并在我的任务完成后发送一个松弛消息,所以我更改了代码:
tasks = [
asyncio.create_task(self._start_update(update, spec))
for update, spec in zip(updates, specs)
]
task = asyncio.create_task(self.notify_as_completed(tasks))
return Report(updates=updates,job_task=asyncio.gather(task))
--------------------------------
async def notify_as_completed(self, tasks) -> None:
try:
execute_response = await asyncio.gather(*tasks)
await self._slack_service.send_msg(execute_response)
except Exception as ex:
logging.error(f"wasn't able to send a slack msg: {ex}")
这是一个好的模式还是有更好的方法?