Coroutine & Subroutine

Coroutine은 Co + Routine이 합쳐진 단어이다.

Corouine is a combination of Co + Routine.

Co : 함께 ( Together )

Routine : 규칙적으로 하는 일의 통상적인 순서나 방법 ( The usual order or method of doing something regularly )

프로그래밍에서의 루틴이라고 하면 특정한 일을 처리하기 위한 일련의 명령 혹은 그 순서 정도로 정리할 수 있겠다.

Routines in programming can be organized into a series of commands or orders to handle a specific task.

또한 우리는 이러한 일련의 명령을 함수라고 하기도한다.

We also call this series of commands a Function.

서브루틴 ( What is Subroutine? )

프로그래밍에서 함수 안에 함수가 있는 경우, 내부에 있는 함수를 서브루틴이라고 할 수 있다.

In programming, if there is a function within the function, the function inside is called a subroutine.

def out_function():
  inside_function()
  return

// Subroutine !
def inside_function():
  pass

우리가 외부함수를 호출하면 내부함수는 외부함수의 안에서 실행될 것을 알 수 있다.

If we call out_function, we can expect inside_function to be called within out_function.

코루틴 ( What is Coroutine ? )

코루틴이란 함께 수행되는 함수이다.

Coroutine is a function that runs together.

일반적으로 함수의 진입점은 함수의 시작 부분이 될 것이다. 그러나 코루틴에서는 아니다.

In general, the entry point of the function will be the beginning of the function. But not in Coroutine.

코루틴은 하나의 코루틴이 다른 코루틴 안에서 수행되는 것이 아니다.

In Coroutine, one Coroutine is not performed in the other.

각각은 서로 다른 함수이며, 함께 수행된다. 그러나 다른 프로세스인 것은 아니다.

Each is a different function and is performed together. However, this does not mean that it is a different process, a different thread.

이들이 하나의 스레드를 점유하고 있을 때, 한 Routine이 다른 Routine에게 스레드 점유 권한을 양보함으로써 함께 수행된다.

When they occupy one thread, it is done together by one Routine giving up the thread occupancy authority to another Routine.

즉, 일반 함수는 한 번 진입하고 한 번 리턴한다면, 코루틴은 여러 개의 진입점과 여러 개의 중단점을 가질 수 있다.

That is, if the general function enters once and returns once, the coroutine may have several entry points and several breakpoints(suspend point).

Return은 함수를 완전히 종료하고 스택 메모리에 할당된 모든 리소스를 해제하는 것을 의미하지만, 중단은 힙 메모리 영역에 다시 재개(resume)하기 위해 필요한 모든 정보 - 코루틴 스테이트(coroutine state) - 를 저장하고 제어권을 호출자에게 다시 넘기는 것을 의미한다.

A return means shutting down the function completely and releasing all resources allocated to the stack memory, but suspend means saving all the information required to resume to the heap memory area - called the coroutine state - and handing control back to the caller.

그리고 누군가가 다시 Resume을 요청을 하면 힙 메모리 영역에 저장되어 있던 코루틴 스테이트의 정보를 가져와 복구하여, 중단 되었던 바로 다음부터 함수를 다시 진행 합니다.

And when someone asks for a resume, it takes the information of the coroutine state that was stored in the heap memory area and recovers it, and then it goes back to the function right after it was interrupted.

비선점형 멀티태스킹과 선점형 멀티태스킹

Non-preemptive Multitasking and Preemptive Multitasking

하나의 Task가 Scheduler로 부터 CPU 사용권을 할당 받았을 때, Scheduler가 강제로 CPU 사용권을 뺏을 수 없으면 비선점형 멀티태스킹이고, 뺏을 수 있으면 선점형 멀티태스킹이다.

When a task is assigned a CPU license by the scheduler, it is non-preemptive multi-tasking if the scheduler cannot forcibly take away the CPU license, and preemptive multi-tasking if it can.

코루틴은 비선점형 멀티태스킹이고, 스레드는 선점형 멀티태스킹이다.

Corouine is ‘Non-preemptive Multitasking’ and thread is ‘Preemptive Multitasking

즉, 코루틴은 병행성(Concurrency)을 제공하지만 병렬성(Parallelism)을 제공하지는 않는다는 의미이다.

That is, it means that coroutine provides concurrency but does not provide parallelism.

병행성과 병렬성 ( Concurrency and Parallelism )

  • Concurrency

    • It seems to be running at the same time.

    • It belongs to the Logical Level.

    • Using Single Core

    • It can operate in sequence rather than in parallel physically.

    • In fact, the CPU is divided into time-sharing so that the user can feel the concurrency.

  • Parallelism

    • It is actually done at the same time.

    • It belongs to the Physical (Machine) Level.

    • Only in Multi Core.

코루틴의 장점 ( Advantage of using Coroutine )

코루틴간의 작업 교환 때 비용이 적다는 것이다.

It is said that the cost is low when exchanging work between coroutines.

쓰레드간 작업 교환은 system call 또는 blocking call 콜 등의 비용이 발생한다.

Work exchange between threads occur costs such as system call or blocking call.

동기화 작업을 위한 mutexes, semaphores 등의 장치가 필요없고 OS의 지원도 필요가 없다

There is no need for devices such as mutexes and semaphores for synchronization work and no need for OS support

Last updated