【六】協(xié)程并發(fā)
定義tasks時可以設(shè)置多個ensure畏腕,也可以像多線程那樣用append方法實現(xiàn)
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)))
當(dāng)遇到阻塞時可以使用await讓其他協(xié)程繼續(xù)工作
例如:
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)
通過運(yùn)行時間可以看出aysncio實現(xiàn)了并發(fā)。asyncio.wait(tasks) 也可以使用 asyncio.gather(*tasks) ,前者接受一個task列表茉稠,后者接收一堆task描馅。
【七】協(xié)程嵌套
使用async可以定義協(xié)程,協(xié)程用于耗時的io操作而线,我們也可以封裝更多的io操作過程铭污,這樣就實現(xiàn)了嵌套的協(xié)程,即一個協(xié)程中await了另外一個協(xié)程吞获,如此連接起來况凉。
例如:
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創(chuàng)建協(xié)程對象,那么await的返回值就是協(xié)程運(yùn)行的結(jié)果各拷。
#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協(xié)程函數(shù)里處理結(jié)果刁绒,直接返回await的內(nèi)容,那么最外層的run_until_complete將會返回main協(xié)程的結(jié)果烤黍。
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方式掛起協(xié)程知市。
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)
由此可見傻盟,協(xié)程的調(diào)用和組合十分的靈活,我們可以發(fā)揮想象盡情的浪
【六】協(xié)程并發(fā)
定義tasks時可以設(shè)置多個ensure嫂丙,也可以像多線程那樣用append方法實現(xiàn)
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)))</pre>
當(dāng)遇到阻塞時可以使用await讓其他協(xié)程繼續(xù)工作
例如:
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)</pre>
通過運(yùn)行時間可以看出aysncio實現(xiàn)了并發(fā)娘赴。asyncio.wait(tasks) 也可以使用 asyncio.gather(*tasks) ,前者接受一個task列表,后者接收一堆task跟啤。
【七】協(xié)程嵌套
使用async可以定義協(xié)程诽表,協(xié)程用于耗時的io操作,我們也可以封裝更多的io操作過程隅肥,這樣就實現(xiàn)了嵌套的協(xié)程竿奏,即一個協(xié)程中await了另外一個協(xié)程,如此連接起來腥放。
例如:
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)</pre>
如果使用的是 asyncio.gather創(chuàng)建協(xié)程對象泛啸,那么await的返回值就是協(xié)程運(yùn)行的結(jié)果。
#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)</pre>
不在main協(xié)程函數(shù)里處理結(jié)果秃症,直接返回await的內(nèi)容候址,那么最外層的run_until_complete將會返回main協(xié)程的結(jié)果。
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)</pre>
或者返回使用asyncio.wait方式掛起協(xié)程种柑。
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)</pre>
也可以使用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)</pre>
由此可見岗仑,協(xié)程的調(diào)用和組合十分的靈活,我們可以發(fā)揮想象盡情的浪