- Hands-On Reactive Programming with Python
- Romain Picard
- 383字
- 2021-06-24 18:25:17
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.
- Learning OpenDaylight
- 嵌入式Linux開發技術
- Linux系統架構與運維實戰
- 零起點學Linux系統管理
- Windows Server 2012 Hyper-V:Deploying the Hyper-V Enterprise Server Virtualization Platform
- Linux從零開始學(視頻教學版)
- Arch Linux Environment Setup How-to
- Learn Helm
- 精通Linux內核開發
- NetDevOps入門與實踐
- Linux設備驅動開發
- Kali Linux高級滲透測試(原書第3版)
- Introduction to R for Quantitative Finance
- 統信UOS應用開發進階教程
- Linux網絡操作系統項目教程(RHEL 7.4/CentOS 7.4)(第3版)(微課版)