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.

https://kukuta.tistory.com/222

일반적으둜 ν•¨μˆ˜μ˜ μ§„μž…μ μ€ ν•¨μˆ˜μ˜ μ‹œμž‘ 뢀뢄이 될 것이닀. κ·ΈλŸ¬λ‚˜ μ½”λ£¨ν‹΄μ—μ„œλŠ” μ•„λ‹ˆλ‹€.

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