aiohttp AppRunner的用法
参考廖雪峰的aiohttp教程,会出现两个DeprecationWarning,
- loop argument is deprecated
- Application.make_handler(...) is deprecated, use AppRunner API instead
解决方案
- loop 参数直接不用就是
- 参见aiohttp官网,AppRunner的用法:
The simple startup code for serving HTTP site on 'localhost', port 8080 looks like:
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', 8080)
await site.start()
# -*- coding: utf-8 -*-
import logging
logging.basicConfig(level=logging.INFO)
import os,json,time,asyncio
from datetime import datetime
from aiohttp import web
# def index(request): # 原始简单的url处理函数
# return web.Response(body=b'<h1>Awesome</h1>',content_type='text/html')
def init_jinja2(app, **kw): #初始化 jinja2的 env
pass
async def logger_factory(app, handler):
async def logger(request):
logging.info('Request: %s %s' % (request.method, request.path))
return (await handler(request))
return logger
# 生产 post 提交的数据
async def data_factory(app, handler):
pass
#将url处理函数的返回值 转换成 response 对象
async def response_factory(app, handler):
pass
return response
# 将blog 评论的发布时间 转换成 多少时间以前
def datetime_filter(t):
delta = int(time.time() - t)
if delta < 60:
return u'1分钟前'
if delta < 3600:
return u'%s分钟前' % (delta // 60)
if delta < 86400:
return u'%s小时前' % (delta // 3600)
if delta < 604800:
return u'%s天前' % (delta // 86400)
dt = datetime.fromtimestamp(t)
return u'%s年%s月%s日' % (dt.year, dt.month, dt.day)
async def init(loop):
db = configs.configs.db
await orm.create_pool(loop=loop, **db)
#DeprecationWarning: loop argument is deprecated
app = web.Application(loop = loop,middlewares=[ #拦截器 一个URL在被某个函数处理前,可以经过一系列的middleware的处理。
logger_factory, response_factory #工厂模式
])
init_jinja2(app, filters=dict(datetime=datetime_filter))
add_routes(app, 'handlers')
add_static(app)
# DeprecationWarning: Application.make_handler(...) is deprecated, use AppRunner API instead
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '192.168.2.101', 9000)
logging.info('server started at http://192.168.2.101:9000...')
await site.start()
#以前的写法
# srv = await loop.create_server(app.make_handler(), '192.168.2.101', 9000)
# logging.info('server started at http://192.168.2.101:9000...')
# return srv
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()
aiohttp AppRunner的用法的更多相关文章
- 深入理解协程(四):async/await异步爬虫实战
本文目录: 同步方式爬取博客标题 async/await异步爬取博客标题 本片为深入理解协程系列文章的补充. 你将会在从本文中了解到:async/await如何运用的实际的爬虫中. 案例 从CSDN上 ...
- Python开发【模块】:aiohttp(一)
AIOHTTP 用于asyncio和Python的异步HTTP客户端/服务器 主要特点: 支持客户端和HTTP服务器. 支持服务器WebSockets和 客户端WebSockets开箱即用,没有回调地 ...
- python 调用aiohttp
1. aiohttp安装 pip3 install aiohttp 1.1. 基本请求用法 async with aiohttp.get('https://github.com') as r: a ...
- 异步:asyncio和aiohttp的一些应用(1)
1. asyncio 1.1asyncio/await 用法 async/await 是 python3.5中新加入的特性, 将异步从原来的yield 写法中解放出来,变得更加直观. 在3.5之前,如 ...
- Python使用asyncio+aiohttp异步爬取猫眼电影专业版
asyncio是从pytohn3.4开始添加到标准库中的一个强大的异步并发库,可以很好地解决python中高并发的问题,入门学习可以参考官方文档 并发访问能极大的提高爬虫的性能,但是requests访 ...
- 异步协程asyncio+aiohttp
aiohttp中文文档 1. 前言 在执行一些 IO 密集型任务的时候,程序常常会因为等待 IO 而阻塞.比如在网络爬虫中,如果我们使用 requests 库来进行请求的话,如果网站响应速度过慢,程序 ...
- 小爬爬4.协程基本用法&&多任务异步协程爬虫示例(大数据量)
1.测试学习 (2)单线程: from time import sleep import time def request(url): print('正在请求:',url) sleep() print ...
- Python调用aiohttp
1. aiohttp安装 pip install aiohttp 1.1. 基本请求用法 async with aiohttp.get('https://github.com') as r: awai ...
- python 基于aiohttp的异步爬虫实战
钢铁知识库,一个学习python爬虫.数据分析的知识库.人生苦短,快用python. 之前我们使用requests库爬取某个站点的时候,每发出一个请求,程序必须等待网站返回响应才能接着运行,而在整个爬 ...
随机推荐
- 免费赠送原创的opengl电子书教程和案例源码
免费赠送原创的opengl电子书和案例源码,有兴趣qq群 52391108 下面是一部分教程截图
- VNPY 软件架构分析
VNPY 架构设计文档 版本日志: 一. 引言 编写目的: 该软件概要设计说明书是“VNPY”开发中的重要文献资料,所完成的工作是将需求说明书中的数据流图转化成软件结构和数据结构,为后续进行详细设计说 ...
- 《用Python写爬虫》学习笔记(一)
注:纯文本内容,代码独立另写,属于本人学习总结,无任何商业用途,在此分享,如有错误,还望指教. 1.为什么需要爬虫? 答:目前网络API未完全放开,所以需要网络爬虫知识. 2.爬虫的合法性? 答:爬虫 ...
- 使用Python统计函数绘制复杂图形matplotlib
一.堆积图 1.堆积柱状图 如果将函数bar()中的参数bottom的取值设定为列表y.列表y1代表另一个数,函数bar(x,y1,bottom=y,color="r")就会输出堆 ...
- 电脑小白和ta的小白电脑——MySQL的简单使用
数据库的基本操作:增删改查 分别使用cmd环境和Navicat可视化工具连接MySQL数据库来实现 这里默认了已经安装和配置MySQL↓ https://www.cnblogs.com/gifted3 ...
- java入门day03
控制流程 一: if else / switch 1. 随机数的产生:导入import java.lang.Math; num=Math.random() -->[0,1); [ ...
- 单总线(1-Wire Bus)技术及其应用
单总线(1-Wire Bus)技术及其应用 reference: http://www.doc88.com/p-2106151016582.html 摘要:介绍了美DALLAS公司推出的单总线技术的原 ...
- 【转】forbids in-class initialization of non-const static member不能在类内初始化非const static成员
转自:forbids in-class initialization of non-const static member不能在类内初始化非const static成员 今天写程序,出现一个新错误,好 ...
- css flex布局,小程序flex布局,垂直居中完美解决
flex弹性布局,很好的解决了垂直居中的问题,上代码: wxml: <view class='container'> <view class='item item1'>item ...
- Ceph Luminous版本创建OSD无法激活问题
一.问题描述 部署ceph的Luminous之ceph-12.2.10版本,在其中一个集群节点部署OSD时,发现命令执行完毕后,集群中并没有将格式化的盘加入到OSD中,也没有任何报错,问题如下: [r ...