10python入门——asynico异步

asynico模块

功能

  • 异步网络操作
  • 并发
  • 协程

关键字解释

  • event_loop 事件循环:程序开启一个无限循环,把一些函数注册到事件循环上,当满足事件发生的时候,调用相应的协程函数
  • coroutine 协程:协程对象,指一个使用async关键字定义的函数,它的调用不会立即执行函数,而是会返回一个协程对象。协程对象需要注册到事件循环,由事件循环调用。
  • task 任务:一个协程对象就是一个原生可以挂起的函数,任务则是对协程进一步封装,其中包含了任务的各种状态
  • future: 代表将来执行或没有执行的任务的结果。它和task上没有本质上的区别
  • async/await 关键字:python3.5用于定义协程的关键字,async定义一个协程,await用于挂起阻塞的异步调用接口。

第一次尝试

import time
import asyncio

# 这是定义异步函数,先记住写法即可
async def hello():
    for i in range(5):
        time.sleep(1)
        print('Hello World:%s' % time.time())

# 创建一个事件loop
loop = asyncio.get_event_loop()
# 将协程加入到事件循环loop
loop.run_until_complete(hello())

创建一个task

创建task后,在task加入事件循环之前为pending状态,当完成后,状态为finished

# -*- coding:utf-8 -*-
# Author : ZRQ
# Data : 2019/6/22 17:09

import asyncio
import time

now = lambda: time.time()

async def do_some_work(x):
    print("waiting:", x)

start = now()

coroutine = do_some_work(2)
# 创建一个事件loop
loop = asyncio.get_event_loop()
# 创建task
task = loop.create_task(coroutine)
# task = asyncio.ensure_future(coroutine)
print(task)
# 将task加入事件循环
loop.run_until_complete(task)
print(task)
print("Time:",now()-start)

为任务绑定回调函数

# -*- coding:utf-8 -*-
# Author : ZRQ
# Data : 2019/6/22 17:09

import time
import asyncio

now = lambda : time.time()

async def do_some_work(x):
    print("waiting:",x)
    return "Done after {}s".format(x)

def callback(future):
    print("callback:",future.result())

start = now()
coroutine = do_some_work(2)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(coroutine)
print(task)
# 绑定回调函数
task.add_done_callback(callback)
print(task)
loop.run_until_complete(task)

print("Time:", now()-start)

>
cb=[callback() at D:/Users/Victor/Documents/GitHub/PythonBase/asyncio模块/03task回调.py:14]>
waiting: 2
callback: Done after 2s
Time: 0.006999492645263672

阻塞和异步await

使用async可以定义协程对象,使用await可以针对耗时的操作进行挂起,就像生成器里的yield一样,函数让出控制权。协程遇到await,事件循环将会挂起该协程,执行别的协程,等await后面的操作处理完再回来

# -*- coding:utf-8 -*-
# Author : ZRQ
# Data : 2019/6/22 17:14

import asyncio
import time

now = lambda :time.time()

async def do_some_work(x):
    print("waiting:",x)
    # await 后面就是调用耗时的操作
    await asyncio.sleep(x)
    print(x)
    return "Done after {}s".format(x)

start = now()

coroutine1 = do_some_work(2)
coroutine2 = do_some_work(0)
loop = asyncio.get_event_loop()
tasks = [
    asyncio.ensure_future(coroutine1),
    asyncio.ensure_future(coroutine2)
]
loop.run_until_complete(asyncio.wait(tasks))
# 2个协程都执行完,才进行下面语句
for task in tasks:
    print("Task ret:",task.result())

print("Time:", now() - start)

从输出0,看出遇到阻塞,就执行下一个协程
waiting: 2
waiting: 0
0
2
Task ret: Done after 2s
Task ret: Done after 0s
Time: 2.009284734725952

并行

2种方式获取函数返回值

# -*- coding:utf-8 -*-
# Author : ZRQ
# Data : 2019/6/22 19:05

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)

waiting: 1
waiting: 2
waiting: 4
Task ret: Done after 1s
Task ret: Done after 2s
Task ret: Done after 4s
Time: 4.009031534194946

# -*- coding:utf-8 -*-
# Author : ZRQ
# Data : 2019/6/22 19:07

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)

协程停止处理

先遍历取消事件,停止循环,在重启事件循环,最后在close,不然还会抛出异常

# -*- coding:utf-8 -*-
# Author : ZRQ
# Data : 2019/6/22 19:09

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(2)

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

start = now()

loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(asyncio.wait(tasks))
except KeyboardInterrupt as e:
    print(asyncio.Task.all_tasks())
    for task in asyncio.Task.all_tasks():
        print(task.cancel())
    loop.stop()
    loop.run_forever()
finally:
    loop.close()

print("Time:",now()-start)

线程加协程

实现动态添加协程

# -*- coding:utf-8 -*-
# Author : ZRQ
# Data : 2019/6/22 18:35

import asyncio
import time
from threading import Thread

now = lambda :time.time()

def start_loop(loop):
    asyncio.set_event_loop(loop)
    loop.run_forever()

async def do_some_work(x):
    print('Waiting {}'.format(x))
    await asyncio.sleep(x)
    print('Done after {}s'.format(x))

def more_work(x):
    print('More work {}'.format(x))
    time.sleep(x)
    print('Finished more work {}'.format(x))

start = now()
new_loop = asyncio.new_event_loop()
t = Thread(target=start_loop, args=(new_loop,))
t.start()
print('TIME: {}'.format(time.time() - start))
# 不阻塞
asyncio.run_coroutine_threadsafe(do_some_work(6), new_loop)
asyncio.run_coroutine_threadsafe(do_some_work(4), new_loop)
#阻塞
#new_loop.call_soon_threadsafe(more_work, 6)
#new_loop.call_soon_threadsafe(more_work, 3)

主线程中创建一个new_loop,然后在另外的子线程中开启一个无限事件循环。 主线程通过run_coroutine_threadsafe新注册协程对象。这样就能在子线程中进行事件循环的并发操作

你可能感兴趣的:(Python基础)