python——asyncio模块实现协程、异步编程(二)

【六】协程并发

定义tasks时可以设置多个ensure,也可以像多线程那样用append方法实现

tasks = [
    asyncio.ensure_future(coroutine1),
    asyncio.ensure_future(coroutine2),
    asyncio.ensure_future(coroutine3)
]

for i in range(4, 6):  
    tasks.append(asyncio.ensure_future(do_some_work(i)))

当遇到阻塞时可以使用await让其他协程继续工作

例如:

import asyncio
import time
now = lambda: time.time()
 
async def do_some_work(x):
    print('Waiting: ', x)
 
    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)
 
coroutine1 = do_some_work(1)
coroutine2 = do_some_work(2)
coroutine3 = do_some_work(3) 

tasks = [
    asyncio.ensure_future(coroutine1),
    asyncio.ensure_future(coroutine2),
    asyncio.ensure_future(coroutine3)
]

for i in range(4, 6):  
    tasks.append(asyncio.ensure_future(do_some_work(i)))
 
loop = asyncio.get_event_loop()

start = now()
loop.run_until_complete(asyncio.wait(tasks))
 
for task in tasks:
    print('Task ret: ', task.result())
 
print('TIME: ', now() - start)

通过运行时间可以看出aysncio实现了并发。asyncio.wait(tasks) 也可以使用 asyncio.gather(*tasks) ,前者接受一个task列表,后者接收一堆task。

【七】协程嵌套

使用async可以定义协程,协程用于耗时的io操作,我们也可以封装更多的io操作过程,这样就实现了嵌套的协程,即一个协程中await了另外一个协程,如此连接起来。

例如:

import asyncio
import time
now = lambda: time.time()
async def do_some_work(x):
    print('Waiting: ', x)
 
    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)
 
async def main():
    coroutine1 = do_some_work(1)
    coroutine2 = do_some_work(2)
    coroutine3 = do_some_work(4)
 
    tasks = [
        asyncio.ensure_future(coroutine1),
        asyncio.ensure_future(coroutine2),
        asyncio.ensure_future(coroutine3)
    ]
 
    dones, pendings = await asyncio.wait(tasks)
 
    for task in dones:
        print('Task ret: ', task.result())
 
start = now()
 
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
 
print('TIME: ', now() - start)

如果使用的是 asyncio.gather创建协程对象,那么await的返回值就是协程运行的结果。

    #dones, pendings = await asyncio.wait(tasks)
        #for task in dones:
        #print('Task ret: ', task.result())
    results = await asyncio.gather(*tasks)
    for result in results:
        print('Task ret: ', result)

不在main协程函数里处理结果,直接返回await的内容,那么最外层的run_until_complete将会返回main协程的结果。

import asyncio
import time
now = lambda: time.time()
async def do_some_work(x):
    print('Waiting: ', x)
 
    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)
 
async def main():
    coroutine1 = do_some_work(1)
    coroutine2 = do_some_work(2)
    coroutine3 = do_some_work(4)
 
    tasks = [
        asyncio.ensure_future(coroutine1),
        asyncio.ensure_future(coroutine2),
        asyncio.ensure_future(coroutine3)
    ]
 
    return await asyncio.gather(*tasks)
 
start = now()
loop = asyncio.get_event_loop()
results = loop.run_until_complete(main())
for result in results:
    print('Task ret: ', result)
 
print('TIME: ', now() - start)

或者返回使用asyncio.wait方式挂起协程。

import asyncio
import time
now = lambda: time.time()
async def do_some_work(x):
    print('Waiting: ', x)
 
    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)
 
async def main():
    coroutine1 = do_some_work(1)
    coroutine2 = do_some_work(2)
    coroutine3 = do_some_work(4)
 
    tasks = [
        asyncio.ensure_future(coroutine1),
        asyncio.ensure_future(coroutine2),
        asyncio.ensure_future(coroutine3)
    ]
 
    return await asyncio.wait(tasks)
 
start = now()
 
loop = asyncio.get_event_loop()
done, pending = loop.run_until_complete(main())
 
for task in done:
    print('Task ret: ', task.result())
 
print('TIME: ', now() - start)

也可以使用asyncio的as_completed方法

import asyncio
import time
now = lambda: time.time()
async def do_some_work(x):
    print('Waiting: ', x)
 
    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)
 
async def main():
    coroutine1 = do_some_work(1)
    coroutine2 = do_some_work(2)
    coroutine3 = do_some_work(4)
 
    tasks = [
        asyncio.ensure_future(coroutine1),
        asyncio.ensure_future(coroutine2),
        asyncio.ensure_future(coroutine3)
    ]
    for task in asyncio.as_completed(tasks):
        result = await task
        print('Task ret: {}'.format(result))
 
start = now()
 
loop = asyncio.get_event_loop()
done = loop.run_until_complete(main())
print('TIME: ', now() - start)

由此可见,协程的调用和组合十分的灵活,我们可以发挥想象尽情的浪

【六】协程并发

定义tasks时可以设置多个ensure,也可以像多线程那样用append方法实现

tasks = [
    asyncio.ensure_future(coroutine1),
    asyncio.ensure_future(coroutine2),
    asyncio.ensure_future(coroutine3)
]

for i in range(4, 6):  
    tasks.append(asyncio.ensure_future(do_some_work(i)))

当遇到阻塞时可以使用await让其他协程继续工作

例如:

import asyncio
import time
now = lambda: time.time()

async def do_some_work(x):
    print('Waiting: ', x)

    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)

coroutine1 = do_some_work(1)
coroutine2 = do_some_work(2)
coroutine3 = do_some_work(3) 

tasks = [
    asyncio.ensure_future(coroutine1),
    asyncio.ensure_future(coroutine2),
    asyncio.ensure_future(coroutine3)
]

for i in range(4, 6):  
    tasks.append(asyncio.ensure_future(do_some_work(i)))

loop = asyncio.get_event_loop()

start = now()
loop.run_until_complete(asyncio.wait(tasks))

for task in tasks:
    print('Task ret: ', task.result())

print('TIME: ', now() - start)
python——asyncio模块实现协程、异步编程(二)_第1张图片
image

通过运行时间可以看出aysncio实现了并发。asyncio.wait(tasks) 也可以使用 asyncio.gather(*tasks) ,前者接受一个task列表,后者接收一堆task。

【七】协程嵌套

使用async可以定义协程,协程用于耗时的io操作,我们也可以封装更多的io操作过程,这样就实现了嵌套的协程,即一个协程中await了另外一个协程,如此连接起来。

例如:

import asyncio
import time
now = lambda: time.time()
async def do_some_work(x):
    print('Waiting: ', x)

    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)

async def main():
    coroutine1 = do_some_work(1)
    coroutine2 = do_some_work(2)
    coroutine3 = do_some_work(4)

    tasks = [
        asyncio.ensure_future(coroutine1),
        asyncio.ensure_future(coroutine2),
        asyncio.ensure_future(coroutine3)
    ]

    dones, pendings = await asyncio.wait(tasks)

    for task in dones:
        print('Task ret: ', task.result())

start = now()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

print('TIME: ', now() - start)
python——asyncio模块实现协程、异步编程(二)_第2张图片
image

如果使用的是 asyncio.gather创建协程对象,那么await的返回值就是协程运行的结果。

#dones, pendings = await asyncio.wait(tasks)
        #for task in dones:
        #print('Task ret: ', task.result())
    results = await asyncio.gather(*tasks)
    for result in results:
        print('Task ret: ', result)
python——asyncio模块实现协程、异步编程(二)_第3张图片
image

不在main协程函数里处理结果,直接返回await的内容,那么最外层的run_until_complete将会返回main协程的结果。

import asyncio
import time
now = lambda: time.time()
async def do_some_work(x):
    print('Waiting: ', x)

    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)

async def main():
    coroutine1 = do_some_work(1)
    coroutine2 = do_some_work(2)
    coroutine3 = do_some_work(4)

    tasks = [
        asyncio.ensure_future(coroutine1),
        asyncio.ensure_future(coroutine2),
        asyncio.ensure_future(coroutine3)
    ]

    return await asyncio.gather(*tasks)

start = now()
loop = asyncio.get_event_loop()
results = loop.run_until_complete(main())
for result in results:
    print('Task ret: ', result)

print('TIME: ', now() - start)
python——asyncio模块实现协程、异步编程(二)_第4张图片
image

或者返回使用asyncio.wait方式挂起协程。

import asyncio
import time
now = lambda: time.time()
async def do_some_work(x):
    print('Waiting: ', x)

    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)

async def main():
    coroutine1 = do_some_work(1)
    coroutine2 = do_some_work(2)
    coroutine3 = do_some_work(4)

    tasks = [
        asyncio.ensure_future(coroutine1),
        asyncio.ensure_future(coroutine2),
        asyncio.ensure_future(coroutine3)
    ]

    return await asyncio.wait(tasks)

start = now()

loop = asyncio.get_event_loop()
done, pending = loop.run_until_complete(main())

for task in done:
    print('Task ret: ', task.result())

print('TIME: ', now() - start)
python——asyncio模块实现协程、异步编程(二)_第5张图片
image

也可以使用asyncio的as_completed方法

import asyncio
import time
now = lambda: time.time()
async def do_some_work(x):
    print('Waiting: ', x)

    await asyncio.sleep(x)
    return 'Done after {}s'.format(x)

async def main():
    coroutine1 = do_some_work(1)
    coroutine2 = do_some_work(2)
    coroutine3 = do_some_work(4)

    tasks = [
        asyncio.ensure_future(coroutine1),
        asyncio.ensure_future(coroutine2),
        asyncio.ensure_future(coroutine3)
    ]
    for task in asyncio.as_completed(tasks):
        result = await task
        print('Task ret: {}'.format(result))

start = now()

loop = asyncio.get_event_loop()
done = loop.run_until_complete(main())
print('TIME: ', now() - start)
python——asyncio模块实现协程、异步编程(二)_第6张图片
image

由此可见,协程的调用和组合十分的灵活,我们可以发挥想象尽情的浪

image

原文地址:http://m.blog.csdn.net/sm9sun/article/details/75446875

你可能感兴趣的:(python——asyncio模块实现协程、异步编程(二))