Python学习 15day__高级语法
asyncio
asyncio 本身是一个消息循环
步骤:
创建消息循环
把协程导入
关闭
1 import threading 2 # 引入异步io包 3 import asyncio 4 5 # 使用协程 6 @asyncio.coroutine 7 def hello(): 8 print("Hello Word! (%s)"% threading.currentThread()) 9 print("Start.....(%s)"% threading.currentThread()) 10 yield from asyncio.sleep(10) 11 print("Done....(%s)"% threading.currentThread()) 12 print("Hello again! (%s)" % threading.currentThread()) 13 14 # 启动消息循环 15 loop = asyncio.get_event_loop() 16 # 定义任务 17 tasks = [hello(), hello()] 18 # asyncio使用wait等待task执行完毕 19 loop.run_until_complete(asyncio.wait(tasks)) 20 # 关闭消息循环 21 loop.close()
1 import asyncio 2 3 @asyncio.coroutine 4 def wget(host): 5 print("wget %s..." % host) 6 # 异步请求网络地址 7 connect = asyncio.open_connection(host, 80) 8 # 注意yield from 的用法 9 reader, write = yield from connect 10 header = 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' % host 11 write.write(header.encode('utf-8')) 12 yield from write.drain() 13 while True: 14 line = yield from reader.readline() 15 # http协议的换行使用\r\n 16 if line == b'\r\n': 17 break 18 print('%s header > %s' % (host, line.decode('utf-8').rstrip())) 19 20 write.close() 21 22 loop = asyncio.get_event_loop() 23 tasks = [wget(host) for host in ['www.baidu.com']] 24 loop.run_until_complete(asyncio.wait(tasks)) 25 loop.close()
async and await
为了更好的的表示异步io
让协程代码更简洁
使用上,可以简单的进行替换
用async替换¥ asyncio。coroutine
用await替换yield from
aiohttp
asyncio 实现单线程的并发io,在客户端用处不大
在服务器端可以asyncio+coroutine配合,因为http是io操作
asyncio实现了tcp, udp,ssl等协议
aiohttp是给予astncio实现的http框架
pip install aiohttp安装
concurrent.futures
类似其他语言的线程池的概念
利用multiprocessiong实现真正的并行计算
核心原理:以子进程的形式,并行运行多个python解释器,从而令python程序可以利用多核cpu来提升执行速度
1 from concurrent.futures import ThreadPoolExecutor 2 import time 3 4 def return_future(msg): 5 time.sleep(3) 6 return msg 7 8 # 创建一个线程池 9 pool = ThreadPoolExecutor(max_workers = 2) 10 11 # 向线程池加入两个task 12 f1 = pool.submit(return_future, 'hello') 13 f2 = pool.submit(return_future, 'world') 14 # 等待执行完毕 15 print(f1.done()) 16 time.sleep(3) 17 print(f2.done()) 18 # 结果 19 print(f1.result()) 20 print(f2.result())
current 中map函数
跟map函数类似
函数需要异步执行
timeout:超时时间
map跟submit使用一个就行
1 import time, re 2 import os,datetime 3 4 from concurrent import futures 5 6 data = ['1', '2'] 7 8 def wait_on(argument): 9 print(argument) 10 time.sleep(2) 11 return 'ok' 12 13 ex = futures.ThreadPoolExecutor(max_workers = 2) 14 for i in ex.map(wait_on, data): 15 print(i)