# 协程 + 事件循环
import asyncio
async def func(): # async关键字不能和yield一起使用,引入coroutine装饰器来装饰downloader生成器。
print('start')
# 异步调用asyncio.sleep(1):
await asyncio.sleep(1) # time.sleep 异步的接口 对象 必须实现__await__
# await 就相当于 yield from
print('end')
# 获取EventLoop:
loop = asyncio.get_event_loop()
task1 = loop.create_task(func())
task2 = loop.create_task(func())
# 执行coroutine
loop.run_until_complete(asyncio.wait([task1, task2]))
# loop.run_until_complete(func())
# asyncio是一个底层模块
# 他完成了几个任务的轮流检测io,并且在遇到io的时候能够及时在任务之间进行切换
# 然后达到使用单线程实现异步的方式
# aiohttp
# 完全使用asyncio作为底层模块完成的
# loop 你的循环
# 你的协程任务就是 被async语法约束的 函数
# 你的协程任务遇到io才能切出去,如何标识你的任务遇到io了呢? await 对象
# async 语法 帮助我们创建协程任务
# asyncio模块 : 异步io模块 对协程任务的循环管理 内置了很多异步的接口帮助我们 异步的socketserver,采用异步的形式去访问socket server
import asyncio
async def get_url():
reader,writer = await asyncio.open_connection('www.shuoiliu.com',80)
writer.write(b'GET / HTTP/1.1\r\nHOST:www.shuoiliu.com\r\nConnection:close\r\n\r\n')
all_lines = []
async for line in reader:
data = line.decode()
all_lines.append(data)
html = '\n'.join(all_lines)
return html
async def main():
tasks = []
for url in range(2):
tasks.append(asyncio.ensure_future(get_url()))
for res in asyncio.as_completed(tasks):
result = await res
print(result)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main()) # 处理一个任务
loop.run_until_complete(asyncio.wait([main()])) # 处理多个任务
task = loop.create_task(main()) # 使用create_task获取返回值
loop.run_until_complete(task)
loop.run_until_complete(asyncio.wait([task]))
import asyncio
# 执行任务获取返回值
'''
async def hello():
print("Hello world!")
await asyncio.sleep(1)
print("Hello again!")
return 'done'
loop = asyncio.get_event_loop()
task = loop.create_task(hello())
loop.run_until_complete(task)
ret = task.result()
print(ret)
'''
# 执行多个任务按照返回的顺序获取返回值
import asyncio
async def hello(i):
print("Hello world!",i)
await asyncio.sleep(i)
print("Hello again!")
return 'done', i
async def main():
tasks = []
for i in range(5):
tasks.append(asyncio.ensure_future(hello((20 - i) / 10)))
for res in asyncio.as_completed(tasks):
result = await res
print(result)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())