当我转到asyncio页面时,第一个例子是hello程序。当我在python 3.73上运行它时,我看不出与普通的有什么不同,有人能告诉我其中的区别并给出一个非平凡的例子吗?
In [1]: import asyncio
...:
...: async def main():
...: print('Hello ...')
...: await asyncio.sleep(5)
...: print('... World!')
...:
...: # Python 3.7+
...: asyncio.run(main())
Hello ...
... World!
In [2]:
In [2]: import time
...:
...: def main():
...: print('Hello ...')
...: time.sleep(5)
...: print('... World!')
...:
...: # Python 3.7+
...: main()
Hello ...
... World!我故意把时间从1秒增加到5秒,希望看到一些特别的东西,但我没有。
发布于 2019-06-24 06:28:55
您没有看到什么特别的东西,因为您的代码中没有太多的异步工作。然而,主要的区别是time.sleep(5)是阻塞的,而asyncio.sleep(5)是非阻塞的.
当time.sleep(5)被调用时,它将阻止整个脚本的执行,它将被暂停,只是冻结,什么也不做。但是,当您调用await asyncio.sleep(5)时,它将要求事件循环在等待语句完成其执行时运行其他内容。
下面是一个改进的例子。
import asyncio
async def hello():
print('Hello ...')
await asyncio.sleep(1)
print('... World!')
async def main():
await asyncio.gather(hello(), hello())
asyncio.run(main())将产出:
~$ python3.7 async.py
Hello ...
Hello ...
... World!
... World!您可以看到,await asyncio.sleep(1)没有阻止脚本的执行。
相反,将行await asyncio.sleep(1)替换为time.sleep(1),输出将是
Hello ...
... World!
Hello ...
... World!因为time.sleep是阻塞的,而hello()的第一个调用必须在第二个hello()调用开始运行之前先完成。
希望它有帮助:)
发布于 2022-10-26 03:42:02
使用下面的test1(),首先每一秒运行一次time.sleep(1),然后每一秒运行一次time.sleep(1):
import asyncio
import time
async def test1():
for _ in range(0, 3):
print('Test1')
time.sleep(1) # Here
async def test2():
for _ in range(0, 3):
print('Test2')
time.sleep(1) # Here
async def main():
await asyncio.gather(test1(), test2()) # Here
asyncio.run(main())因此,test1()和test2()总共运行了6秒:
Test1 # 1 second
Test1 # 2 seconds
Test1 # 3 seconds
Test2 # 4 seconds
Test2 # 5 seconds
Test2 # 6 seconds使用下面的test1()和test2(),每一秒交替运行一次:
import asyncio
async def test1():
for _ in range(0, 3):
print('Test1')
await asyncio.sleep(1) # Here
async def test2():
for _ in range(0, 3):
print('Test2')
await asyncio.sleep(1) # Here
async def main():
await asyncio.gather(test1(), test2()) # Here
asyncio.run(main())因此,运行test1()和test2()总共只需要3秒:
Test1 # 1 second
Test2 # 1 second
Test1 # 2 seconds
Test2 # 2 seconds
Test1 # 3 seconds
Test2 # 3 seconds使用下面的test1(),首先立即运行time.sleep(0),然后立即运行test2():
import asyncio
import time
async def test1():
for _ in range(0, 3):
print('Test1')
time.sleep(0) # Here
async def test2():
for _ in range(0, 3):
print('Test2')
time.sleep(0) # Here
async def main():
await asyncio.gather(test1(), test2()) # Here
asyncio.run(main())因此,test1()和test2()总共运行0秒:
Test1 # 0 second
Test1 # 0 second
Test1 # 0 second
Test2 # 0 second
Test2 # 0 second
Test2 # 0 second而且,在下面的test1()和test2()中,可以交替运行asyncio.sleep(0):
import asyncio
async def test1():
for _ in range(0, 3):
print('Test1')
await asyncio.sleep(0) # Here
async def test2():
for _ in range(0, 3):
print('Test2')
await asyncio.sleep(0) # Here
async def main():
await asyncio.gather(test1(), test2()) # Here
asyncio.run(main())因此,总共只需要运行test1()和test2()的0秒:
Test1 # 0 second
Test2 # 0 second
Test1 # 0 second
Test2 # 0 second
Test1 # 0 second
Test2 # 0 second最后,在没有下面的test1()或asyncio.sleep()的情况下,首先立即运行time.sleep(),然后立即运行test2():
import asyncio
async def test1():
for _ in range(0, 3):
print('Test1')
async def test2():
for _ in range(0, 3):
print('Test2')
async def main():
await asyncio.gather(test1(), test2()) # Here
asyncio.run(main())因此,test1()和test2()总共运行0秒:
Test1 # 0 second
Test1 # 0 second
Test1 # 0 second
Test2 # 0 second
Test2 # 0 second
Test2 # 0 secondhttps://stackoverflow.com/questions/56729764
复制相似问题