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

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.

主站蜘蛛池模板: 南皮县| 桓台县| 木里| 镇康县| 德州市| 新干县| 胶州市| 德安县| 繁峙县| 内乡县| 长白| 九寨沟县| 积石山| 日照市| 漯河市| 托克托县| 远安县| 宣化县| 伽师县| 娄底市| 武隆县| 宣武区| 吴忠市| 阿拉善盟| 青海省| 万源市| 枣庄市| 哈密市| 昌黎县| 盐津县| 珲春市| 乌兰浩特市| 海淀区| 修水县| 长沙市| 潜山县| 灵璧县| 彝良县| 崇礼县| 兴义市| 罗山县|