미완성상태
# Chapter06-03
# 흐름제어, 병행성(Concurrency)
# 코루틴(Coroutine)
# yield : 메인 <-> 서브
# 코루틴 제어, 상태, 양방향 전송
# yield from
# 서브루틴 : 메인루틴에서 호출 -> 서브루틴에서 수행(흐름제어)
# 코루틴 : 루틴 실행 중 중지 -> 동시성 프로그래밍
# 코루틴 : 쓰레드에 비해 오버헤드 감소
# 쓰레드 : 싱글쓰레드 -> 멀티쓰레드 -> 복잡 -> 공유되는 자원 -> 교착 상태 발생 가능성, 컨텍스트 스위칭 비용 발생, 자원 소비 가능성 증가
기본형식
간단히 메세지타입data를 메인<---->서브 끼리 주고 받는 형식이다.
코루틴함수(서브)쪽은 "변수 = yield" 형식으로 짜고
Main함수쪽(메인)은 next( )함수로 Start시켜주고
함수명.send( data ) 로 코루틴함수(서브)쪽에 data를 보낸다. 이때 보낸 data는 "yield" 대신에 들어간다
그다음에 next()함수로 다음 "yield"를 넘어갈 수 도 있고 그다음 차례 "yield"에 보내려면 send해주면된다.
1 코루틴 함수 이런식일때
2_1 이렇게하면 이렇게 출력
2_2 이렇게하면 이렇게 출력
코드
# Chapter06-03
# 흐름제어, 병행성(Concurrency)
# 코루틴(Coroutine)
# yield : 메인 <-> 서브
# 코루틴 제어, 상태, 양방향 전송
# yield from
# 서브루틴 : 메인루틴에서 호출 -> 서브루틴에서 수행(흐름제어)
# 코루틴 : 루틴 실행 중 중지 -> 동시성 프로그래밍
# 코루틴 : 쓰레드에 비해 오버헤드 감소
# 쓰레드 : 싱글쓰레드 -> 멀티쓰레드 -> 복잡 -> 공유되는 자원 -> 교착 상태 발생 가능성, 컨텍스트 스위칭 비용 발생, 자원 소비 가능성 증가
# 코루틴 Ex1
def coroutine1():
print('>>> coroutine started.')
i = yield
print('>>> coroutine received : {}'.format(i))
# 제네레이터 선언
cr1 = coroutine1()
print(cr1, type(cr1))
# yield 지점 까지 서브루틴 수행
# next(cr1)
# 기본 전달 값 None
# next(cr1)
# 값 전송
# cr1.send(100)
# 잘못된 사용
cr2 = coroutine1()
# cr2.send(100) # 예외 발생
# 코루틴 Ex2
# GEN_CREATED : 처음 대기 상태
# GEN_RUNNING : 실행 상태
# GEN_SUSPENDED : yield 대기 상태
# GEN_CLOSED : 실행 완료 상태
def coroutine2(x):
print('>>> coroutine started : {}'.format(x))
y = yield x
print('>>> coroutine received : {}'.format(y))
z = yield x + y
print('>>> coroutine received : {}'.format(z))
cr3 = coroutine2(10)
from inspect import getgeneratorstate
print(getgeneratorstate(cr3))
print(next(cr3))
print(getgeneratorstate(cr3))
print(cr3.send(15))
# print(c3.send(20)) # 예외
print()
print()
# 코루틴 Ex3
# StopIteration 자동 처리(3.5 -> await)
# 중첩 코루틴 처리
def generator1():
for x in 'AB':
yield x
for y in range(1,4):
yield y
t1 = generator1()
print(next(t1))
print(next(t1))
print(next(t1))
print(next(t1))
print(next(t1))
# print(next(t1))
t2 = generator1()
print(list(t2))
print()
print()
def generator2():
yield from 'AB'
yield from range(1,4)
t3 = generator2()
print(next(t3))
print(next(t3))
print(next(t3))
print(next(t3))
print(next(t3))
# print(next(t3))
print()
print()
'C++,python (인프런+사이트) > python 파이썬 정리' 카테고리의 다른 글
코루틴 메인<--->서브 주고 받는법 (0) | 2022.04.04 |
---|---|
Generator 제너레이터 ( Yield ) (0) | 2022.04.04 |
데코레이터 (0) | 2022.04.03 |
Closure 클로저 (0) | 2022.04.03 |
Class로 구현하는 클로저 기능 (0) | 2022.04.03 |
댓글