Reputation: 888
Hey guys so I have this function in python that is async and after finishing returns a value but this function is added as a task as many times as there is items in a certain list the thing is I dont seem to be able to get the values returned from such functions after asyncio.wait()
finishes running.
The code (I removed most of the bloat so this is a simplified code):
async def transcribe(s3_path: str, file_name: str):
for attempt in range(MAX_WAIT_ATTEMPTS):
# Do some stuff
await asyncio.sleep(WAIT_TIME)
return "value 1", "value 2"
async def transcribe_all_videos(videos_list: List[str], user_id: str):
tasks = []
for video in videos_list:
tasks.append(
asyncio.get_event_loop().create_task(transcribe("some path", "some video"))
)
result = await asyncio.wait(tasks)
# Extract result return data
return result
What I tried:
result[0].result()
result.result()
None of those seem to work and the error is always in the lines of 'tuple' object has no attribute 'result'
Upvotes: 1
Views: 1329
Reputation: 195633
asyncio.wait
returns done and pending tasks. So iterate over done tasks and use the .result()
method:
import asyncio
async def transcribe(s3_path: str, file_name: str):
await asyncio.sleep(1)
return "value 1", "value 2"
async def transcribe_all_videos(videos_list, user_id):
tasks = []
for video in videos_list:
tasks.append(
asyncio.get_event_loop().create_task(
transcribe("some path", "some video")
)
)
done, pending = await asyncio.wait(tasks)
for r in done:
print(r.result())
asyncio.run(transcribe_all_videos(["xxx"], 1))
Prints:
('value 1', 'value 2')
Upvotes: 1