官术网_书友最值得收藏!

Coroutines

Coroutines are another building block of AsyncIO. Coroutines were already covered in detail previously in this chapter. A coroutine is basically a generator with syntactic sugar. A coroutine is an asynchronous function that can be interrupted and resumed at specific locations. A coroutine is declared the same way as a function, but with the async keyword prefixed:

import datetime

async def wait(delay):
now = datetime.datetime.now()
print("wait for {} seconds at {}:{}:{}".format(
delay, now.hour, now.minute, now.second))
return True

This coroutine just returns True for now. The first big difference between a coroutine and a regular function is the fact that its code is not immediately executed when the coroutine is called. Instead, a coroutine object is returned:

wait(3)
<coroutine object wait at 0x10b807570>

The second difference between a coroutine and a function is the fact that a coroutine can call another coroutine, and so it can be interrupted and resumed at these code locations. Calling another coroutine, and asynchronously waiting for its result, is done with the await keyword. For example, the wait coroutine can wait for some time without blocking the whole program by using the asyncio.sleep coroutine:

import asyncio
import datetime

async def wait(delay):
now = datetime.datetime.now()
print("wait for {} seconds at {}:{}:{}".format(
delay, now.hour, now.minute, now.second))
await asyncio.sleep(delay)
now = datetime.datetime.now()
print("waited for {} seconds at {}:{}:{}".format(
delay, now.hour, now.minute, now.second))
return True

The await syntax indicates to the Python interpreter that when the sleep coroutine is called, then the execution of the wait coroutine must be interrupted. The execution of the wait coroutine resumes when the sleep coroutine completes. This means that the wait coroutine continues its execution after the await expression, with all its context (local variables and closures) being restored. So, let's try to run this wait coroutine. Executing a coroutine and retrieving its result is done with the await keyword, used before the call itself. But there is a catch; this works only inside of a coroutine. So, trying to use the await keyword outside of a coroutine does not work:

await wait(3)
File "<stdin>", line 1
await wait(3)
^
SyntaxError: invalid syntax

This is the typical chicken and egg issue. A coroutine can only be called by another coroutine. So how can someone bootstrap a first call to a coroutine? This is the role of the event loop.

主站蜘蛛池模板: 北京市| 藁城市| 攀枝花市| 祥云县| 温州市| 伊吾县| 永嘉县| 浮山县| 白沙| 蒲城县| 河南省| 鹿泉市| 建水县| 连云港市| 民勤县| 繁昌县| 澄江县| 镇远县| 土默特右旗| 梅州市| 鹿泉市| 鄂尔多斯市| 平昌县| 清徐县| 武城县| 临西县| 乐山市| 托克托县| 舒兰市| 江安县| 原平市| 临西县| 耒阳市| 安塞县| 宣威市| 阜康市| 平顶山市| 清水县| 游戏| 陆川县| 印江|