Python中的asyncio.sleep()与time.sleep()的比较

ztmd8pv5  于 2022-11-26  发布在  Python
关注(0)|答案(2)|浏览(233)

当我进入asyncio页面时,第一个例子是hello world程序。当我在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秒,希望看到一些特别的东西,但我没有。

nwwlzxa7

nwwlzxa71#

您没有看到任何特别的东西,因为代码中没有太多的异步工作。但是,主要的区别是time.sleep(5)是阻塞的,而asyncio.sleep(5)是非阻塞的。
time.sleep(5)被调用时,它会阻塞脚本的整个执行过程,并被暂停,只是冻结,什么也不做。但是当你调用await asyncio.sleep(5)时,它会要求事件循环在await语句完成执行时运行其他的东西。
下面是一个改进的示例。

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()的第二个调用开始运行之前先完成。
希望对你有帮助:)

ovfsdjhp

ovfsdjhp2#

在下面的time.sleep(1)中,首先每隔一秒运行test1(),然后每隔一秒运行**test2()**:

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

在下面的**asyncio.sleep(1)中,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

并且,使用下面的time.sleep(0),首先,立即运行**test1(),然后立即运行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

并且,使用下面的asyncio.sleep(0),**test1()test2()**同时交替运行:

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

最后,如果没有下面的time.sleep()asyncio.sleep(),则首先立即运行**test1(),然后立即运行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 second

相关问题