이 예제 코드의 목적은 코루틴 내에서 새로운 태스크들을 시작시키는 것이다. 태스크들에 대해
대기하지 않아도 코루틴 함수
f
()
내의 실행 콘텍스트와 별개로 태스크들이 동작할 것이다. 또
한
f
()
는 시작시킨 태스크들이 완료되기 전에 먼저 종료할 것이다.
파이썬
3
.
7
이전에는 태스크를 스케줄링하기 위해
loop
인스턴스를 먼저 획득해야 했지만
get
_
running
_
loop
()
가 도입된 후로는 그것을 사용하는
asyncio
.
create
_
task
()
와 같은
asyncio
함수들만으로 태스크를 생성할 수 있게 되었다. 파이썬
3
.
7
이후의 비동기 태스크 생
성 코드는 [예제
3
-
14
]와 같아졌다.
예제
3-14
현대식으로 태스크 만들기
import asyncio
async def f():
# 태스크 생성
for i in range():
asyncio.create_task(<some other coro>)
태스크를 생성하기 위해
create
_
task
()
외에 저수준 함수인
asyncio
.
ensure
_
future
()
를
사용할 수도 있다. 오래된
asyncio
코드에서 확인할 수 있다. 혼돈을 방지하기 위해
ensure
_
future
()
에 대해 논하는 것을 피하려 했지만,
asyncio
API
사례 중 프레임워크
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.