본문 바로가기
프로그래밍 기초/Python

[Python] 코루틴

by ITPro 2016. 3. 13.

def cor(txt):

    print "coroutine start"

    

    while True:

        line = (yield)  #send로 전달받은 값을 line에 저장

        if txt in line:

            print line

            


c=cor("py") #코루틴 초기화


c.next()    #코루틴 실행

c.send("show me the money") #코루틴에 값 전달

c.send("snoopy")

c.send("black sheep wall")

c.send("python")

c.close()   #코루틴 종료

반응형

'프로그래밍 기초 > Python' 카테고리의 다른 글

[Python] 모듈화  (0) 2016.03.13
[Python] 클래스  (0) 2016.03.13
[Python] 예외처리  (0) 2016.03.08
[Python] 생성기  (0) 2016.03.08
[Python] 파일 입출력  (0) 2016.03.02