await 何时会暂停 python 中可等待对象的执行?
When await will suspend the execution of an awaitable object in python?
我已阅读,接受的答案提到:
It is worth mentioning that asyncio.sleep is explicitly guaranteed to suspend execution and defer to the event loop, even when the specified delay is 0 (in which case it will immediately resume on the next event loop pass)."
来自 python 文档 https://docs.python.org/3/library/asyncio-dev.html#concurrency-and-multithreading:
When a Task executes an await expression, the running Task gets suspended, and the event loop executes the next Task.
但是在我下面的例子中,在do_something
中遇到await asyncio.sleep(0.1)
的时候没有挂起,我的理解有什么问题吗?
import asyncio
async def do_something(i):
await asyncio.sleep(0.1)
print('in do_something')
async def main():
for i in range(10):
print(i)
await do_something(i)
t1 = time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print('cost time:', time.time() - t1)
预期输出:
0
1
2
3
4
5
6
7
8
9
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
cost time: 1.0283539295196533
实际输出:
in do_something
1
in do_something
2
in do_something
3
in do_something
4
in do_something
5
in do_something
6
in do_something
7
in do_something
8
in do_something
9
in do_something
cost time: 1.0283539295196533
But in my following example, it didn't suspend when it meet await asyncio.sleep(0.1)
in do_something
它确实暂停了,从某种意义上说,控制权在睡眠期间传递给了事件循环,允许它执行其他任务——如果有的话。要看到这一点,将 loop.run_until_complete(main())
更改为 loop.run_until_complete(asyncio.gather(main(), main()))
,您将观察到两个主电源并行执行。
正如文档所说,await
的意思是“调用这个异步函数,并暂停我直到它完成”。所以“await”意味着“等待”,如果你只有一个任务,等待会让一切看起来都是同步的。您可以将 await do_something(i)
更改为 asyncio.create_task(do_something(i))
,并在 main 的末尾添加一个 await asyncio.sleep(10)
(以防止程序结束),您应该会看到预期的输出。
我已阅读
It is worth mentioning that asyncio.sleep is explicitly guaranteed to suspend execution and defer to the event loop, even when the specified delay is 0 (in which case it will immediately resume on the next event loop pass)."
来自 python 文档 https://docs.python.org/3/library/asyncio-dev.html#concurrency-and-multithreading:
When a Task executes an await expression, the running Task gets suspended, and the event loop executes the next Task.
但是在我下面的例子中,在do_something
中遇到await asyncio.sleep(0.1)
的时候没有挂起,我的理解有什么问题吗?
import asyncio
async def do_something(i):
await asyncio.sleep(0.1)
print('in do_something')
async def main():
for i in range(10):
print(i)
await do_something(i)
t1 = time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
print('cost time:', time.time() - t1)
预期输出:
0
1
2
3
4
5
6
7
8
9
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
in do_something
cost time: 1.0283539295196533
实际输出:
in do_something
1
in do_something
2
in do_something
3
in do_something
4
in do_something
5
in do_something
6
in do_something
7
in do_something
8
in do_something
9
in do_something
cost time: 1.0283539295196533
But in my following example, it didn't suspend when it meet
await asyncio.sleep(0.1)
indo_something
它确实暂停了,从某种意义上说,控制权在睡眠期间传递给了事件循环,允许它执行其他任务——如果有的话。要看到这一点,将 loop.run_until_complete(main())
更改为 loop.run_until_complete(asyncio.gather(main(), main()))
,您将观察到两个主电源并行执行。
正如文档所说,await
的意思是“调用这个异步函数,并暂停我直到它完成”。所以“await”意味着“等待”,如果你只有一个任务,等待会让一切看起来都是同步的。您可以将 await do_something(i)
更改为 asyncio.create_task(do_something(i))
,并在 main 的末尾添加一个 await asyncio.sleep(10)
(以防止程序结束),您应该会看到预期的输出。