๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“– Note

์ฝ”๋ฃจํ‹ด vs ์Šค๋ ˆ๋“œ

Python network modules๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋‹ค ๋ณด๋‹ˆ, ๊ฐ ๊ฐ์˜ network module๋“ค์ด coroutine์„ ์ด์šฉํ•ด์„œ ๋งŒ๋“ค์—ˆ๋‹ค๋Š” ๋ฌธ์žฅ์„ ์ž์ฃผ ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.

๋”๋ถˆ์–ด Coroutine์ด Thread์™€ ๋น„์Šทํ•˜๊ฒŒ ์‹คํ–‰์ค„๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ ๋™์ž‘์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ๋„,

๊ทธ๋ž˜์„œ ๋‘ ๊ฐ€์ง€์˜ ์ฐจ์ด์ ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

๋‘ ๊ฐ€์ง€์˜ ์ฐจ์ด์ ์„ ์•Œ๊ธฐ์ „์— ์•Œ์•„๋‘ฌ์•ผํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋™์‹œ์„ฑ๊ณผ ๋ณ‘๋ ฌ์„ฑ์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด ์žˆ๋‹ค.

>> 2020/03/05 - [๐Ÿ“– Note] - ๋™์‹œ์„ฑ vs ๋ณ‘๋ ฌ์„ฑ ( Concurrency vs Parallelism )

 

 

At stackoverflow


 

Coroutines are a form of sequential processing: only one is executing at any given time (just like subroutines AKA procedures AKA functions -- they just pass the baton among each other more fluidly).

์ฝ”๋ฃจํ‹ด์€ ์ˆœ์ฐจ ์ฒ˜๋ฆฌ์˜ ํ•œ ํ˜•ํƒœ์ด๋‹ค. ์ฃผ์–ด์ง„ ์‹œ๊ฐ„์— ์˜ค์ง ํ•˜๋‚˜๋งŒ ์‹คํ–‰๋œ๋‹ค.

+ ํ•œ๊ฐœ์˜ ์Šค๋ ˆ๋“œ๋กœ ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ํ‰๋‚ด๋‚ด๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅํ•œ ๊ฐœ๋….

 

Threads are (at least conceptually) a form of concurrent processing: multiple threads may be executing at any given time. (Traditionally, on single-CPU, single-core machines, that concurrency was simulated with some help from the OS -- nowadays, since so many machines are multi-CPU and/or multi-core, threads will de facto be executing simultaneously, not just "conceptually").

๊ฐœ๋…์ ์œผ๋กœ๋Š” ๋™์‹œ ์ฒ˜๋ฆฌ์˜ ํ˜•ํƒœ์ด๋‹ค. ์ฃผ์–ด์ง„ ์‹œ๊ฐ„์— ๋‹น์ค‘ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค. ์˜›๋‚  ์‹ฑ๊ธ€ CPU, ์‹ฑ๊ธ€ ์ฝ”์–ด ๋จธ์‹ ์—์„œ๋Š” OS์˜ ๋„์›€์„ ๋ฐ›์•„ ๋™์‹œ์„ฑ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ํ–ˆ์—ˆ๋Š”๋ฐ ์š”์ฆ˜์—” ๋ฉ€ํ‹ฐ CPU ๋ฐ ๋ฉ€ํ‹ฐ ์ฝ”์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ๋™์‹œ์— ์‹คํ–‰๋œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

Coroutine

C์–ธ์–ด ๋“ฑ์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์‹œ์ž‘ํ•  ๋•Œ ์ง„์ž…ํ•˜๋Š” ์ง€์ ์ด ํ•˜๋‚˜ ์กด์žฌํ•˜๊ณ  ํ•จ์ˆ˜๊ฐ€ ๋ชจ๋‘ ์‹คํ–‰๋˜๊ฑฐ๋‚˜, return ๊ตฌ๋ฌธ์— ์˜ํ•ด ์ข…๋ฃŒ๋˜๋Š” ์ง€์ ์„ ์„ค์ •ํ•  ์ˆ˜์žˆ๋‹ค.

์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋ฅผ Subroutine( ์„œ๋ธŒ๋ฃจํ‹ด )์ด๋ผ ๋ถ€๋ฅด๋Š”๋ฐ, ์ฝ”๋ฃจํ‹ด์€ ์ด๋ฅผ ๋” ์ผ๋ฐ˜ํ™”ํ•œ ๊ฐœ๋…์œผ๋กœ ์ง„์ž…ํ•˜๋Š” ์‹œ์ ๊นŒ์ง€ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๊ฐœ๋…์ ์œผ๋กœ๋งŒ ๋ณธ๋‹ค๋ฉด ์„œ๋ธŒ๋ฃจํ‹ด์˜ ํ•œ ์ข…๋ฅ˜๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ๋‹ค.

 

C#, JavaScript, Python ๋“ฑ์ด ์ œ๊ณตํ•˜๋Š” Generator๊ฐ€ ์‚ฌ์‹ค ์ฝ”๋ฃจํ‹ด์˜ ํ•œ ํ˜•ํƒœ์ด๋‹ค.

 

caller๊ฐ€ ํ•จ์ˆ˜๋ฅผ callํ•˜๊ณ , ํ•จ์ˆ˜๊ฐ€ caller์—๊ฒŒ ๊ฐ’์„ returnํ•˜๋ฉด์„œ ์ข…๋ฃŒํ•˜๋Š” ๊ฒƒ์— ๋”ํ•ด returnํ•˜๋Š” ๋Œ€์‹  suspend(ํ˜น์€ yield)ํ•˜๋ฉด caller๊ฐ€ ๋‚˜์ค‘์— resumeํ•˜์—ฌ ์ค‘๋‹จ๋œ ์ง€์ ๋ถ€ํ„ฐ ์‹คํ–‰์„ ์ด์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

 

์ฆ‰, ์ฝ”๋ฃจํ‹ด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์ฝ”๋ฃจํ‹ด ํ•จ์ˆ˜ ๋‚ด๋ถ€์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์žˆ๋‹ค๊ฐ€ suspend ํ˜น์€ yield์— ์˜ํ•ด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ ๋ถ€๋ถ„์— ๊ฐ’์ด ๋ฐ˜ํ™˜๋˜๊ณ  ์ดํ›„ ๋‹ค์‹œ ์ฝ”๋ฃจํ‹ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด ์ฝ”๋ฃจํ‹ด ํ•จ์ˆ˜ ๋‚ด๋ถ€์— suspend ํ˜น์€ yield ์ง€์  ์ดํ›„ ์ฝ”๋“œ๋กœ ๋Œ์•„์™€์„œ ์‹คํ–‰์„ ํ•˜๊ฒŒ ๋œ๋‹ค๋Š” ๋ง์ด๋‹ค.

 

๋ถ€๋ถ„์ ์œผ๋กœ, ๊ทธ๋ฆฌ๊ณ  ํŠน์ •ํ•œ ์ƒํ™ฉ์ด ๋งž์•„ ๋–จ์–ด์กŒ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜.

 

- ์žฅ์ 

1. ์„ฑ๋Šฅ

์ผ๋ฐ˜์ ์œผ๋กœ ๊ฒŒ์ž„์—์„œ ์ฝ”๋ฃจํ‹ด์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋งค ํ”„๋ ˆ์ž„๋งˆ๋‹ค Update๊ตฌ๋ฌธ์—์„œ ๋™์ž‘์„ ํ™•์ธํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ฝ”๋ฃจํ‹ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋ฃจํ‹ด์ด ์ง€์ •ํ•œ ์‹œ๊ฐ„์ด ํ›„์— ์ž๋™์œผ๋กœ ํ™•์ธ๋œ๋‹ค.

์ด๋Ÿฌํ•œ ์  ๋•Œ๋ฌธ์— ํŠนํžˆ ๋ชจ๋ฐ”์ผ ๊ธฐ๊ธฐ์—์„œ ์ฝ”๋ฃจํ‹ด์˜ ํ™œ์šฉ์€ ์„ฑ๋Šฅ ํ–ฅ์ƒ์— ํฐ ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.

2. ๊ฐ€๋…์„ฑ

์ฝ”๋“œ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง„๋‹ค. ์ด๊ฑด ์จ๋ณด๋ฉด ์•Œ ๊ฒƒ ์ด๋‹ค.

 

- ํŠน์„ฑ

* ํŠน์ • ์ž‘์—…์„ ๋‹จ๊ณ„์ ์œผ๋กœ ๋ฐœ์ƒํ•˜๊ฒŒ ํ•œ๋‹ค.

* ์‹œ๊ฐ„ ํ๋ฆ„์— ๋”ฐ๋ผ ๋ฐœ์ƒํ•˜๋Š” ๋ฃจํ‹ด์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

* ๋‹ค๋ฅธ ์—ฐ์‚ฐ์ด ์™„๋ฃŒ๋ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋ฃจํ‹ด์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

* ๋น„๋™๊ธฐ๊ฐ€ ์•„๋‹ˆ๋‹ค. = ๋™์‹œ์— ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

>> ์–ธ์–ด๋ณ„๋กœ ์ฝ”๋ฃจํ‹ด์„ ์‚ฌ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค 

 

Thread

ํ˜„์žฌ์— ์™€์„œ ์Šค๋ ˆ๋“œ์˜ ๊ฒฝ์šฐ๋Š” ๋ฉ€ํ‹ฐ์ฝ”์–ด๋ฅผ ๋„˜์–ด์„œ many ์ฝ”์–ด ์‹œ๋Œ€๋กœ ์ „ํ™˜ ๋˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ์ž์›๋“ค์„ ์ถฉ๋ถ„ํžˆ ๋ชจ๋‘ ์ด์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋ณ‘๋ ฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ์˜ ํ•ต์‹ฌ๋„๊ตฌ๊ฐ€ ์Šค๋ ˆ๋“œ ์ด๋‹ค.

 

๋ฉ€ํ‹ฐํ”„๋กœ์„ธ์Šค ๊ธฐ๊ณ„์—์„œ, ์Šค๋ ˆ๋“œ๋Š” ์‹ค์ œ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์™€ ํ•จ๊ป˜ ๋™์‹œ์— ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰ ๋  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•˜๊ธฐ ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค.

๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๊ฐ€ ํŠน์ • ๋ถ€๋ถ„์„ ์ฝ๊ณ  ์žˆ๋Š” ๋™์‹œ์— ๊ทธ๊ฒƒ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์ด ๋•Œ๋ฌธ์— ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ์˜์—ญ์„ ๋งŒ๋“ค์ง€ ์•Š๊ฑฐ๋‚˜ ๊ณต์œ ๋œ ์ž์›์„ ์ฝ๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ์‹œ์ผœ์•ผ ๋˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด, ๊ณต์œ ๋œ ์ž์›์œผ๋กœ ๋ถ€ํ„ฐ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋ฅผ ์ž ๊ถˆ๋ฒ„๋ฆผ์œผ๋กœ์จ( Lock ) ๋ฌธ์ œ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ณ  ์žˆ๋‹ค.

 

์Šค๋ ˆ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ํ•˜๋‚˜์˜ ํ”„๋กœ๊ทธ๋žจ์—์„œ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์ผ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋™์‹œ์— ๋งŽ์€ ์ผ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

- ํŠน์„ฑ

* ๋น„๋™๊ธฐ ์ด๋‹ค. = ๋™์‹œ์— ๋ฐœ์ƒํ•œ๋‹ค.

 

 


Thanks for