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库爬取某个站点的时候,每发出一个请求,程序必须等待网站返回响应才能接着运行,而在整个爬 ...
随机推荐
- SQL SEVER 开窗函数总结
作为一名刚刚入门的开发人员,要学的东西很多很多,有些无从下手.秉着“问题是病.技术是药.对症下药”的原则,将工作中遇到的问题所需的技术进行梳理.归纳和总结. 一.什么是开窗函数 首先,什么是开窗函数, ...
- padding属性很有用
html代码中的text-align有时失效,特别是用bootstrap时,用padding-left:xx%,能够很好定位,而且只能够电脑和手机浏览器显示的统一.
- sqlserver数据库方面的排序四大王
--1. row_number 用法 这个函数的功能是为查询出来的每一行记录生成一个序号select row_number() over(order by stuid) as row_number,* ...
- C# json转model 以及model转json
1.json转model TestModel tm = new TestModel(); JavaScriptSerializer js = new JavaScriptSerializer();tm ...
- eclipese的一些卡顿问题
一,在jsp页面上输入拼音的时候很卡顿怎么办? 二,我们在复制粘贴等用到ctry的时候会很卡顿,尤其是在jsp页面.干掉以下就好了.
- riakKV 配置
安装好riakKV之后, 我们需要将 riak.conf 文件中的 nodename修改为本机的 IP 地址. nodename = xx@xxx.xxx.xx.xx listener.http.in ...
- sass中文注释的解决方法和一些简单用法
最近用sass来编写项目中的css,发现不能添加中文注释,报错如下 于是查阅了一下发现需要在scss文件顶部加上@charset "utf-8"即可解决. 在此顺便记录一些sass ...
- Shadow Properties之美(一)【Microsoft Entity Framework Core随笔】
最近在做公司的项目的时候,开始把部分程序迁移到EF Core,然后有了一些感触,趁着还没忘却,还是先记录下来. EF Core还在成长中,我写这个的时候,版本是2.2.如果对着已有的EF 5/6来说, ...
- 机器学习 xgboost 笔记
一.数据预处理.特征工程 类别变量 labelencoder就够了,使用onehotencoder反而会降低性能.其他处理方式还有均值编码(对于存在大量分类的特征,通过监督学习,生成数值变量).转换处 ...
- 记一次 SSM 分页
1.实体层(entity,pojo,domain) package com.entity; import java.io.Serializable; private int totalCount; / ...