Skip to content
Minsing Log
Go back

English Edition

Results Don't Come Out in Order in Async

Programming & CS

Why Don’t Index Values Get Mixed Up in Async Processing?

-> The results of asynchronous processing come out jumbled!!

I understand your question about whether tasks in asynchronous processing proceed in order or proceed in a jumbled order. Due to the nature of asynchronous processing, tasks can be executed simultaneously, but the reason the results come out in order is because of how asyncio.gather() works. I’ll explain this in detail now.

Why order is maintained in asynchronous processing

  1. How asyncio.gather() works:

    • asyncio.gather() executes multiple asynchronous tasks simultaneously, but returns the results in the order they were provided. In other words, even if the tasks passed to gather() are executed asynchronously, the result list is arranged in the order the tasks were passed.
    • For example, if there is a task list called tasks = [task1, task2, task3], asyncio.gather(*tasks) returns the results as [result1, result2, result3], in the order the tasks were passed, even if each task is executed asynchronously.
  2. Execution order of asynchronous tasks:

    • Asynchronous tasks can be executed simultaneously, but each task may take a different amount of time to complete. However, asyncio.gather() returns the results in the order they were provided, regardless of the order in which the tasks finish.
    • For example, even if task1 takes 3 seconds, task2 takes 1 second, and task3 takes 2 seconds, asyncio.gather() returns the results in the order [result1, result2, result3]. In other words, regardless of the order in which the tasks finish, the results always come out in the order they were provided.
  3. Parallelism of asynchronous tasks:

    • Asynchronous tasks are executed in parallel, but asyncio.gather() guarantees the order in which the results are returned. Therefore, even if the tasks are executed in a jumbled order, the results always come out in order.

Explanation with example code

import asyncio
import random

async def task(idx):
    delay = random.uniform(1, 3)  # 1초에서 3초 사이의 랜덤한 지연 시간
    await asyncio.sleep(delay)
    print(f"Task {idx} 완료 (지연 시간: {delay:.2f}초)")
    return idx

async def main():
    tasks = [task(i) for i in range(5)]
    results = await asyncio.gather(*tasks)
    print(f"결과: {results}")

# 이벤트 루프 실행
asyncio.run(main())

Example execution result

Task 1 완료 (지연 시간: 1.23초)
Task 3 완료 (지연 시간: 1.45초)
Task 0 완료 (지연 시간: 2.12초)
Task 4 완료 (지연 시간: 2.34초)
Task 2 완료 (지연 시간: 2.89초)
결과: [0, 1, 2, 3, 4]

Conclusion

Additional tip

If you want to receive results in the order the tasks finish, you can use asyncio.as_completed() instead of asyncio.gather(). as_completed() returns results in the order the tasks finish.

import asyncio
import random

async def task(idx):
    delay = random.uniform(1, 3)
    await asyncio.sleep(delay)
    print(f"Task {idx} 완료 (지연 시간: {delay:.2f}초)")
    return idx

async def main():
    tasks = [task(i) for i in range(5)]
    for completed_task in asyncio.as_completed(tasks):
        result = await completed_task
        print(f"완료된 작업 결과: {result}")

# 이벤트 루프 실행
asyncio.run(main())

If you run this code, the results will be printed in the order the tasks finish.

공개 여정 구독

글이 올라오면 이메일로 보내드릴게요.

개발, 창업, 배움, 실패와 회고를 짧고 솔직하게 기록합니다. 검색으로 스쳐가는 독자가 아니라 여정을 따라오는 팬을 만들기 위한 뉴스레터입니다.

뉴스레터는 준비 중입니다. 그동안 RSS 로 구독하거나 이메일 로 알림을 요청해 주세요.


Share this post:

Comments

Sign in with GitHub to leave a comment.


Previous Post
Explaining OpenAI Async Processing: The GOAT
Next Post
How Asynchronous Processing Works