python多任务处理,Python3协作多任务处理中的`await'是吗?

博客探讨了Python3.5引入的异步IO(asyncio)协程如何与1997年现代操作系统课程中提到的合作式多任务相联系。作者指出,Python的await表达式在协程中的使用类似于合作式多任务,因为协程会自愿暂停执行,等待结果,然后在下次唤醒时恢复,保持状态。这一机制在概念上与合作式多任务的运行方式相似。
摘要由CSDN通过智能技术生成

I am trying to understand the new asyncio coroutines (introduced in Python 3.5).

In 1997 I attended a course at university which roughly covered the content of the book Modern Operating Systems by Andrew Tanenbaum.

Somehow the await in Python3 reminds me at Cooperative Multitasking.

From Wikipedia:

Cooperative multitasking, also known as non-preemptive multitasking, is a style of computer multitasking in which the operating system never initiates a context switch from a running process to another process. Instead, processes voluntarily yield control periodically or when idle in order to enable multiple applications to be run simultaneously. This type of multitasking is called "cooperative" because all programs must cooperate for the entire scheduling scheme to work.

If you look at the Python interpreter like an operating system, does the term "Cooperative Multitasking" apply to await?

But may be I am missing something.

解决方案Inside a coroutine function, the await expression can be used to

suspend coroutine execution until the result is available. Any object

can be awaited, as long as it implements the awaitable protocol by

defining the await() method.

A coroutine can pause execution using the await keyword with another coroutine. While it is paused, the coroutine’s state is maintained, allowing it to resume where it left off the next time it is awakened. That sounds quite like Cooperative multitasking to me. See this example

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值