데, 이 코루틴은 네트워크에서 데이터를 보내거나 받으려고 기다려야 할 때마다 제어권을 다시
이벤트 루프로 돌려준다.
이벤트 루프가 살아있는 동안에는 서버에 연결하는 클라이언트마다
finder()
코루틴의 새로
운 인스턴스가 실행된다. 이렇게 함으로써 이 간단한 서버가 여러 클라이언트를 동시에 처리할
수 있게 된다. 이 과정은 서버에서
KeyboardInterrupt
예외가 발생하거나
OS
가 서버 프로세
스를 종료할 때까지 계속된다.
이제
finder()
코루틴이 있는
tcp_mojifinder.py
의 앞부분을 살펴보자.
예제
21-14
tcp
_
mojifinder
.
py
: [예제
21
-
12
]에서 이어짐
import asyncio
import functools
import sys
from asyncio.trsock import TransportSocket
from typing import cast
from charindex import InvertedIndex, format_results ❶
CRLF = b’\r\n’
PROMPT = b’?> ‘
async def finder(index: InvertedIndex, ❷
reader: asyncio.StreamReader,
writer: asyncio.StreamWriter) -> None:
client ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.