๐Ÿ’ป/ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํŒŒ์ด์ฌ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ruhz 2024. 6. 18. 22:11

ํŒŒ์ด์ฌ Generator

 

Generators - Python Wiki

Generator functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop. Simplified Code The simplification of code is a result of generator function and generator expression support provided by Python. To illus

wiki.python.org

 

Coroutine

 

์ฝ”๋ฃจํ‹ด - ์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „

์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „. ์ฝ”๋ฃจํ‹ด(coroutine)์€ ๋ฃจํ‹ด์˜ ์ผ์ข…์œผ๋กœ์„œ, ํ˜‘๋™ ๋ฃจํ‹ด์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค(์ฝ”๋ฃจํ‹ด์˜ "Co"๋Š” with ๋˜๋Š” together๋ฅผ ๋œปํ•œ๋‹ค). ์ƒํ˜ธ ์—ฐ๊ณ„ ํ”„๋กœ๊ทธ๋žจ์„ ์ผ์ปซ๋Š”๋‹ค๊ณ ๋„ ํ‘œํ˜„๊ฐ€๋Šฅํ•˜๋‹ค.

ko.wikipedia.org

 

Coroutine in Python - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

def print_name(prefix):
    print("Searching prefix:{}".format(prefix))
    try : 
        while True:
            name = (yield)
            if prefix in name:
                print(name)
    except GeneratorExit:
        print("Closing coroutine!!")
     
     
print(type(print_name))  # <class 'function'>
print(type(print_name()))  # <class 'generator'>

corou = print_name("Dear")  # Searching prefix:Dear
corou.__next__()
corou.send("Atul")  
corou.send("Dear Atul")  # Dear Atul
corou.close()  # Closing coroutine!!

ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์‹คํ–‰ ์‹œ generator ํด๋ž˜์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. generator ํด๋ž˜์Šค๋กœ ์ผ๋ฐ˜์ ์ธ coroutine์˜ ๊ฐœ๋…์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Native Coroutine

์ดํ›„๋กœ ํŒŒ์ด์ฌ์€, asyncio ๋ชจ๋“ˆ์—์„œ ๋น„๋™๊ธฐ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋ฉฐ Coroutine ํด๋ž˜์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ผ๋ฐ˜์ ์ธ ์ฝ”๋ฃจํ‹ด์˜ ๊ฐœ๋…์„ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋กœ ๊ตฌํ˜„ํ•œ ๊ฒƒ๊ณผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด, ๋„ค์ดํ‹ฐ๋ธŒ ์ฝ”๋ฃจํ‹ด์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. 

 

Coroutines and Tasks

This section outlines high-level asyncio APIs to work with coroutines and Tasks. Coroutines, Awaitables, Creating Tasks, Task Cancellation, Task Groups, Sleeping, Running Tasks Concurrently, Eager ...

docs.python.org

 

Task, Future

 

Futures

Source code: Lib/asyncio/futures.py, Lib/asyncio/base_futures.py Future objects are used to bridge low-level callback-based code with high-level async/await code. Future Functions: Future Object: T...

docs.python.org

 

์šฐ๋ฆฌ๋Š” ๊ฐ์ฒด๊ฐ€ await ํ‘œํ˜„์‹์—์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์„ ๋•Œ ์–ด์›จ์ดํ„ฐ๋ธ” ๊ฐ์ฒด๋ผ๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค. 
๋งŽ์€ asyncio API๋Š” ์–ด์›จ์ดํ„ฐ๋ธ”์„ ๋ฐ›์•„๋“ค์ด๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
์„ธ ๊ฐ€์ง€ ์ฃผ์š” ์œ ํ˜•์ด ์žˆ์Šต๋‹ˆ๋‹ค: ์ฝ”๋ฃจํ‹ด, ํƒœ์Šคํฌ ๋ฐ ํ“จ์ฒ˜.

ํƒœ์Šคํฌ๋Š” ์ฝ”๋ฃจํ‹ด์„ ๋™์‹œ์— ์˜ˆ์•ฝํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
์ฝ”๋ฃจํ‹ด์ด asyncio.create_task()์™€ ๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํƒœ์Šคํฌ๋กœ ์‹ธ์ผ ๋•Œ ์ฝ”๋ฃจํ‹ด์€ ๊ณง ์‹คํ–‰๋˜๋„๋ก ์ž๋™์œผ๋กœ ์˜ˆ์•ฝ๋ฉ๋‹ˆ๋‹ค:

Future๋Š” ๋น„๋™๊ธฐ ์—ฐ์‚ฐ์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํŠน๋ณ„ํ•œ ์ €์ˆ˜์ค€ ์–ด์›จ์ดํ„ฐ๋ธ” ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

Future ๊ฐ์ฒด๋ฅผ ๊ธฐ๋‹ค๋ฆด ๋•Œ, ๊ทธ๊ฒƒ์€ ์ฝ”๋ฃจํ‹ด์ด Future๊ฐ€ ๋‹ค๋ฅธ ๊ณณ์—์„œ ํ•ด๊ฒฐ๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆด ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.
์ฝœ๋ฐฑ ๊ธฐ๋ฐ˜ ์ฝ”๋“œ๋ฅผ async/await์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ ค๋ฉด asyncio์˜ Future ๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.
์ผ๋ฐ˜์ ์œผ๋กœ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ์ˆ˜์ค€ ์ฝ”๋“œ์—์„œ Future ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

๋•Œ๋•Œ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์ผ๋ถ€ asyncio API์— ์˜ํ•ด ๋…ธ์ถœ๋˜๋Š” Future ๊ฐ์ฒด๋ฅผ ๊ธฐ๋‹ค๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

 

'concurrent.futures' vs 'asyncio.futures'

 

๋น„๋™๊ธฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Asyncio ์ดํ•ด

concurrent.futures(๋‹ค์ค‘ ์Šค๋ ˆ๋“œ, ๋น„๋™๊ธฐ/Non-block ๋ฐฉ์‹) Asyncio๋ฅผ ์ดํ•ดํ•˜๊ธฐ ์ „์—, thread, process์—์„œ์˜ concurrent ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์œผ๋กœ python 3.2์— ์ถ”๊ฐ€๋œ future(PEP 3148 – futures - execute computations asynchronously)๋ฅผ

bentist.tistory.com

 

Futures

Source code: Lib/asyncio/futures.py, Lib/asyncio/base_futures.py Future objects are used to bridge low-level callback-based code with high-level async/await code. Future Functions: Future Object: T...

docs.python.org

 

concurrent.futures — Launching parallel tasks

Source code: Lib/concurrent/futures/thread.py and Lib/concurrent/futures/process.py The concurrent.futures module provides a high-level interface for asynchronously executing callables. The asynchr...

docs.python.org